2016-09-29 39 views
1

我有一个名为SocialPlatform类:可以Python类与父CLASSE共享变量

class SocialPlatform: 
    nb_post = 0 

    def __init__(self, data): 
     self.data = data 
     self._init_type() 
     self.id_post = self._init_id_post() 

    @classmethod 
    def _init_id_post(cls): 
     cls.nb_post += 1 
     return cls.nb_post 

,并从SocialPlatform继承threee其他类:

class Facebook(SocialPlatform): 
    _NAME = 'facebook' 

    @classmethod 
    def get_class_name(cls): 
     return cls._NAME 

    # code here 


class Twitter(SocialPlatform): 
    _NAME = 'twitter' 

    @classmethod 
    def get_class_name(cls): 
     return cls._NAME 

    # code here 


class Instagram(SocialPlatform): 
    _NAME = 'instagram' 

    @classmethod 
    def get_class_name(cls): 
     return cls._NAME 

    # code here 

我的想法是,每次递增nb_postSocialPlatform的实例已创建。我因子评分这个变量是所有从SocialPlatform

继承所以我测试了,在我的主要功能的类之间共享:

def main(): 
    post = Post() # an other class with stuff in it, it doesn't matter here 
    social_platform = { 
     'facebook': Facebook, 
     'twitter': Twitter, 
     'instagram': Instagram 
    } 
    while True: 
     try: 
      platform = social_platform[post.actual_post['header']['platform']](post.actual_post['data']) 
     except KeyError: 
      print 'Platform (%s) not implemented yet' % post.actual_post['header']['platform'] 
      sys.exit(84) 

     print 'platform name : ' + platform.get_class_name() 
     print 'post id : ' + str(platform.id_post) 
     # platform.aff_content() 

     post.pop() 
     if not len(post.post): 
      break 

     print 'enter return to display next post' 
     while raw_input() != "": pass 

但是当我使用这个代码,我得到这样的输出:

platform name : twitter 
post id : 1 
enter return to display next post 

platform name : facebook 
post id : 1 
enter return to display next post 

platform name : twitter 
post id : 2 

使用此方法nb_post在Twitter,Facebook或Instagram实例之间共享,而不是全部。

所以我的问题是:有没有办法在Python中做到这一点?

回答

1

当属性为没有找到,它会在更高层次上查找。分配时,尽管使用最本地的级别。

例如:

class Foo: 
    v = 1 

a = Foo() 
b = Foo() 

print(a.v) # 1: v is not found in "a" instance, but found in "Foo" class 
Foo.v = 2 # modifies Foo class's "v" 
print(a.v) # 2: not found in "a" instance but found in class 
a.v = 3 # creates an attribute on "a" instance, does not modify class "v" 
print(Foo.v) # 2 
print(b.v) # 2: not found in "b" instance but found in "Foo" class 

这里_init_id_post被宣布为classmethod,和你做cls.nb_post = cls.nb_post + 1。 在这个表达式中,第一次出现将参考SocialPlatform,然后您在cls对象上指定对象,它指的是TwitterInstagram类,而不是SocialPlatform。 当您在同一个班级上再次呼叫时,第二个出现的cls.nb_post将不会参考SocialPlatform,因为您已在类别Twitter级别创建属性(例如)。

该解决方案是不使用cls但使用SocialPlatform.nb_post += 1(并使其成为@staticmethod

1
class A(): 
     n = 0 

     def __init__(self): 
      A.n += 1 



    class B(A): 

     def __init__(self): 
      super(B, self).__init__() 


    class C(A): 

     def __init__(self): 
      super(C, self).__init__() 


a = A() 
print(a.n) #prints 1 
b = B() 
print(a.n) #prints 2 
c = C() 
print(a.n) #prints 3 

我认为你可以自己计算剩余的。祝你好运!

0

这个工作对我来说:

class SocialPlatform(object): 
    nb_post = 0 
    def __init__(self): 
    self.id_post = A.nb_post 
    A.increment() 

    @classmethod 
    def increment(cls): 
    cls.nb_post += 1 

class Facebook(SocialPlatform): 
    pass 

class Twitter(SocialPlatform): 
    pass 

然后:

>>> a = Facebook() 
>>> b = Twitter() 
>>> c = Twitter() 
>>> 
>>> a.id_post 
0 
>>> b.id_post 
1 
>>> c.id_post 
2