私はSocialPlatform
をという名前のクラスがあります。缶Pythonのクラスは、親クラッセで変数を共有する
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_post
をインクリメントすることでした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
私は、この変数がだから私は私の主な機能であることをテストし
SocialPlatform
から継承するすべてのクラスの間で共有されていました取り払わ
nb_post
は、Twitter、Facebook、Instagramインスタンス間で共有されています。
私の質問です:これはPythonでこれを行う方法はありますか?