在Python类中使用super()多次

时间:2011-04-14 16:43:48

标签: python

为什么user.params()不会返回继承中的所有参数 链? - 它不包括Person()中定义的参数 - 通知 Vertex()没有params()方法。


class Element(object):
   def __init__(self,element_type):
        self.oid = None
        self.uuid = uuid.uuid4()
        self.key = None
        self.element_type = element_type

   def params(self):
       return dict(uuid=self.uuid, key=self.key)

class Vertex(Element):
   def __init__(self):
       super(Vertex,self).__init__("vertex")

class Person(Vertex):

   def __init__(self,name=None,uri=None,email=None):
       self.s = super(Person,self)
       self.s.__init__()
       self.name=name
       self.uri=uri
       self.email = email

   def params(self):
       params = dict(name=self.name,uri=self.uri,email=self.email)
       params.update(self.s.params())
       return params

class User(Person):

   def __init__(self,
                name=None,
                uri=None,
                email=None,
                first_name=None,
                last_name=None,
                facebook_id=None,
                facebook_link=None,
                facebook_username=None,
                gender=None,
                locale=None):
       self.s = super(User,self)
       self.s.__init__(name,uri,email)

       self.first_name = first_name
       self.last_name = last_name
       self.facebook_id = facebook_id
       self.facebook_link = facebook_link
       self.facebook_username = facebook_username
       self.gender = gender
       self.locale = locale

   def params(self):
       params = dict(first_name=self.first_name,
                     last_name=self.last_name,
                     facebook_id=self.facebook_id,
                     facebook_link=self.facebook_link,
                     facebook_username=self.facebook_username,
                     gender=self.gender,
                     locale=self.locale)
       print self.s.params()
       params.update(self.s.params())
       return params

2 个答案:

答案 0 :(得分:2)

在用户中你做:

self.s = super(User,self)
self.s.__init__(name,uri,email)

所以self.s是什么?当你在Person中做同样的事情时,self.s是超级(Person),并且当你重新分配self.s时,在Person和User中的任何地方,所以被选中的self.s.params是Element之一。

答案 1 :(得分:1)

编辑:以下代码也有效,Sebastians有正确的解释:每次在__init__类中重新分配self.s。因此self.s被重新分配为super(Person,self)

import uuid

class Element(object):
   def __init__(self,element_type):
        self.oid = None
        self.uuid = uuid.uuid4()
        self.key = None
        self.element_type = element_type

   def params(self):
       print 'here Element'
       return dict(uuid=self.uuid, key=self.key)

class Vertex(Element):
   def __init__(self):
       super(Vertex,self).__init__("vertex")

class Person(Vertex):

   def __init__(self,name=None,uri=None,email=None):
       super(Person,self).__init__()
       self.name=name
       self.uri=uri
       self.email = email

   def params(self):
       print 'here Person'
       params = dict(name=self.name,uri=self.uri,email=self.email)
       params.update(super(Person,self).params())
       return params

class User(Person):

   def __init__(self,
                name=None,
                uri=None,
                email=None,
                first_name=None,
                last_name=None,
                facebook_id=None,
                facebook_link=None,
                facebook_username=None,
                gender=None,
                locale=None):
       super(User,self).__init__(name,uri,email)
       self.first_name = first_name
       self.last_name = last_name
       self.facebook_id = facebook_id
       self.facebook_link = facebook_link
       self.facebook_username = facebook_username
       self.gender = gender
       self.locale = locale

   def params(self):
       params = dict(first_name=self.first_name,
                     last_name=self.last_name,
                     facebook_id=self.facebook_id,
                     facebook_link=self.facebook_link,
                     facebook_username=self.facebook_username,
                     gender=self.gender,
                     locale=self.locale)
       print 'here User'
       params.update(super(User, self).params())
       return params

if __name__ == '__main__':
   u = User()
   print '\n'.join(sorted(u.params().keys()))