开发者

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:

  1. A subclass is where the class inheriting from another class is called a subclass. To make father a subclass of family, use the syntax class Father(Family):. What you've created here is actually called an Inner Class, not a subclass.

  2. 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 the if __name__ ... line) gets executed when a module is imported.

  3. Similarly, you could make Family a package - which in Python basically means a directory on the filesystem containing an __init__.py file. Father and Mother would then be modules or classes within the package

  4. Possibly what you're trying to achieve is declare that an object of type Family always has a Father object and a Mother 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 and Mother inside Family, they could be defined outside Family and aggregated into it. (Is it the fact, that Father and Mother should not be accessed outside of a Family? 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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜