I wrote the following code trying to figure out how to instantiate the subclasses within the main class.. I came up with something that doesn't feel right.. at least for me.
Is there something wrong with this type of instancing? Is there a better way to call subclasses?
class Family():
def __init__(self):
self.Father = self.Father(self)
self.Mother = self.Mother(self)
class Father():
def __init__(self, instance = ''):
self = instance if instance != '' else self
print self
def method(self):
print "Father Method"
def fatherMethod(self):
print "Father Method"
class Mother():
def __init__(self, instance = ''):
self = instance if instance != '' else self
print self
def method(self):
print "Mother Method"
def motherMethod(self):
print "Mother Method"
if __name__ == "__main__":
Family = Family()
Family.Father.method()
Family.Mother.method()
-
This looks weird. Why are you passing self to the inner-class constructors? Please explain what you want to achieve with this.Vijay Mathew– Vijay Mathew2010年01月07日 10:10:12 +00:00Commented Jan 7, 2010 at 10:10
2 Answers 2
What you've defined there are not (in Python terminology at least) subclasses - they're inner classes, or nested classes. I'm guessing that this isn't actually what you were trying to achieve, but I'm not sure what you did actually want - but here are my four best guesses:
A subclass is where the class inheriting from another class is called a subclass. To make
fathera subclass offamily, use the syntaxclass Father(Family):. What you've created here is actually called an Inner Class, not a subclass.When you see something like
Family.Father.method(), it often means Family is a module and Father is a class in that module. In Python,modulebasically means.py file. A module doesn't have an__init__method, but all code at the top level of the module (such as theif __name__ ...line) gets executed when a module is imported.Similarly, you could make Family a package - which in Python basically means a directory on the filesystem containing an
__init__.pyfile.FatherandMotherwould then be modules or classes within the packagePossibly what you're trying to achieve is declare that an object of type
Familyalways has aFatherobject and aMotherobject. This doesn't require nested classes (in fact, nested classes are a completely bizarre way to do this). Just use:
>>> class Mother(): ... def whoami(self): ... print "I'm a mother" ... >>> class Father(): ... def whoami(self): ... print "I'm a father" ...>>> class Family(): ... def __init__(self): ... self.mother = Mother() ... self.father = Father() ... >>> f = Family()>>> f.father.whoami() I'm a father>>> f.mother.whoami() I'm a mother>>>
1 Comment
You are right, this code does not feel right. My questions would be ..
What are you trying to achieve? There is not need to define
FatherandMotherinsideFamily, they could be defined outsideFamilyand aggregated into it. (Is it the fact, thatFatherandMothershould not be accessed outside of aFamily? Python has no visibility modifiers, e.g. because of a principle that goes: 'we are all grown-up here', meaning that developers should be responsible and assume responsible handling of code ...)Do you really need something like
Class.Class.method? Beside the fact, that method lookups are a little costly, these kind of chains may indicate a wrong axis, meaning you're trying to take hold of functionality from a not very clearly designed point (sorry for being so blurry here.)