Python Instantiating SubClasses
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 = ''):
开发者_如何学JAVA 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()
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
father
a 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,module
basically 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__.py
file.Father
andMother
would then be modules or classes within the packagePossibly what you're trying to achieve is declare that an object of type
Family
always has aFather
object and aMother
object. 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 >>>
Blergh.
Why are Father and Mother nested under Family? There's no reason to do this. Define them outside, then instantiate them inside.
I'm not sure exactly what you want to do. You may want to look into Descriptors, which are a way of defining sub-objects within a clss.
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
Father
andMother
insideFamily
, they could be defined outsideFamily
and aggregated into it. (Is it the fact, thatFather
andMother
should 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.)
精彩评论