Posts

Showing posts with the label class

Proper way to extend Python class

Proper way to extend Python class I'm looking to extend a Panda's DataFrame, creating an object where all of the original DataFrame attributes/methods are in tact, while making a few new attributes/methods available. I also need the ability to convert (or copy) objects that are already DataFrames to my new class. What I have seems to work, but I feel like I might have violated some fundamental convention. Is this the proper way of doing this, or should I even be doing it in the first place? import pandas as pd class DataFrame(pd.DataFrame): def __init__(self, df): df.__class__ = DataFrame # effectively 'cast' Pandas DataFrame as my own the idea being I could then initialize it directly from a Pandas DataFrame, e.g.: df = DataFrame(pd.read_csv(path)) You're mixing up inheritance and composition. Your DataFrame class both "has a" and "is a" pd.DataFrame . – mypetlion Jun 2...

Function to behave differently on class vs on instance

Function to behave differently on class vs on instance I'd like a particular function to be callable as a classmethod, and to behave differently when it's called on an instance. For example, if I have a class Thing , I want Thing.get_other_thing() to work, but also thing = Thing(); thing.get_other_thing() to behave differently. class Thing Thing.get_other_thing() thing = Thing(); thing.get_other_thing() I think overwriting the get_other_thing method on initialization should work (see below), but that seems a bit hacky. Is there a better way? get_other_thing class Thing: def __init__(self): self.get_other_thing = self._get_other_thing_inst() @classmethod def get_other_thing(cls): # do something... def _get_other_thing_inst(self): # do something else Isn't this what we use inheritance for. Define a separate class with your new method - which inherits from parent class, then by scope, instance will return ...

How to implement private method in ES6 class with Traceur [duplicate]

How to implement private method in ES6 class with Traceur [duplicate] This question already has an answer here: I use Traceur Compiler to have advantage with ES6 features now. I want to implement this stuff from ES5: function Animal() { var self = this, sayHi; sayHi = function() { self.hi(); }; this.hi = function() {/* ... */} } Currently traceur does not support private and public keywords (from harmony). And ES6 class syntax does not allow to use simple var (or let ) statements in class body. private public var let The only way that I am find is to simulate privates before class declaration. Something like: var sayHi = function() { // ... do stuff }; class Animal { ... It is better then nothing but as expected you can not pass correct this to private method without apply -ing or bind -ing it every time. this apply bind So, is there any possibility to use private data in ES6 class compatible with traceur compiler? This question has been asked b...

I need to reference a variable from a non static class in a non static class constructor. [c#]

I need to reference a variable from a non static class in a non static class constructor. [c#] I'm trying to make a solar system with natural satellites. Currently I'm drawing a planets relative to a static "Sun" class object, but i wold like to make the class use a position of another planet object and draw relative to that planet. To do that i need to extract the x and y position of that planet object. This is the class constructor of a class i use for drawing planets. Nebesko_Telo merkur = new Nebesko_Telo(Sun.x, Sun.y, 4, 12, 4.090909090909091, 1, 255, 255, 255); // A field initializer cannot reference the non-static field, method, // or property 'Form1.merkur'. Nebesko_Telo venera = new Nebesko_Telo(merkur.x, merkur.y, 1, 23, 1.5, 1, 176, 108, 32); This is the class constructor. public Nebesko_Telo(doubl _rel_tel_x, double _rel_tel_y, double _r, double _or, double _Fi_mult, double _tilt_plant_n...

How to return multiple types of data in C#

How to return multiple types of data in C# I do return different instances of service depending on a enum in my application: public (????) CurrentService { get { switch (CurrentServiceEnum) { case ServiceType.ServiceA: return IoC.ServiceA; case ServiceType.ServiceB: return IoC.ServiceB; case ServiceType.ServiceC: return IoC.ServiceC; } } } Each of that service has it's own implementation of RemoveAccount method, taking different type argument. RemoveAccount internal class ServiceA_AccountDataModel { [..] } internal class ServiceB_AccountDataModel { [..] } internal class ServiceC_AccountDataModel { [..] } public class ServiceA { public void RemoveAccount(ServiceA_AccountDataModel model) { [...] } } public class ServiceB { public void RemoveAccount(ServiceB_AccountDataModel model) { [...] } } public class ServiceC { public void RemoveAccount(ServiceC_A...

How to change file associations in eclipse?

How to change file associations in eclipse? I need to reverse engineer from a *.class file to get the source code. I am using jd-eclipse for this but i am not able to associate *.class files with it. I am using Eclipse version 4.7.3 and cant find where to associate it. *.class jd-eclipse *.class Readme says go to "Window > Preferences > General > Editors > File Associations" but i cant find this option in my eclipse. That is the correct Preferences page and is always present. Note that on macOS the Preferences is on the application menu rather than the 'Windows' menu. – greg-449 Jun 29 at 9:32 By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that ...