How to inherit class methods and have them access member variables in python

I'm kind of new to Python, and I was trying to find a better way to transfer a bunch of messy class methods (which use member variables) into a separate module like Utils.py to clean things up. Method names must still be inherited by the base class, but they must also have access to the methods of the parnet class. I am detailing the background because I think there may be a better python way to solve this problem.

Basically, I need to do something like the following if I tried to do this through inheritance: (I played with super, but I could not solve it that way)

class Graph(GraphUtils): graph_size = 10 def print_size(self): print self.graph_size class GraphUtils(): def set_size(self, new_size) self.graph_size = new_size if __name__ == "__main__": g = Graph() print "Graph default size: " + str(g.graph_size) # This is 10 g.set_size(20) g.print_size() # I want it to print 20, but it'll print the default 10 

I know that there is another way to unify the import of class methods and variables into another class, but I am running risk namespace conflicts.

The method that I used in a similar case, when a separate module was supposed to appear as an β€œadd-on” in our library, looked like this: (the idea of ​​an β€œadd-on” arose from the desire to optionally distribute additional functionality for the Graph class, all this is related to licensing)

 class Graph: ext = None graph_size = 10 def __init__(self): self.ext = Extension() self.ext._graph = self def set_size(self, new_size): self.graph_size = new_size class Extension: _graph = None def process(self): print "Processing graph size: " + str(self._graph.graph_size) if __name__ == "__main__": g = Graph() print "Graph default size: " + str(g.graph_size) # This is 10 g.set_size(20) g.ext.process() # Output: Processing graph size: 20 

It's just interesting that you guys think this is the best approach or if it can be reasonably (safely) done in Python. (2.6 +)

Thanks!

+4
source share
1 answer

The solution to this is to define the variables in the __init __ () method of the class and ensure that the inherited object is initialized.

__ init __ () is a magic class method that is called when a new object is created from the class definition.

 # Inherit object for new-style Python classes (recommended) class GraphUtils(object): # Override the __init__() magic-method. def __init__(self): # Initialize the inherited object by executing its __init__() method. super(GraphUtils, self).__init__() def set_size(self, new_size): self.graph_size = new_size # Inherit GraphUtils class Graph(GraphUtils): def __init__(self): # Initialize the inherited GraphUtils object. super(Graph, self).__init__() # Declare the graph_size variable on creation of a new Graph object. self.graph_size = 10 def print_size(self): print self.graph_size if __name__ == "__main__": g = Graph() # It is recommended to use str.format() instead of string concatonation print "Graph default size: {}".format(g.graph_size) # will print "Graph default size: 10" g.set_size(20) g.print_size() # This will print 20 

http://docs.python.org/reference/datamodel.html#basic-customization

http://docs.python.org/glossary.html#term-new-style-class

http://docs.python.org/library/functions.html#super

http://docs.python.org/library/string.html#string-formatting

+4
source

All Articles