can python classes share variable with parent classe
<p>I have a class named <code>SocialPlatform</code> :</p>

<pre><code>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
</code></pre>

<p>and threee other classes that inherit from <code>SocialPlatform</code> :</p>

<pre><code>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
</code></pre>

<p>My idea was to increment <code>nb_post</code> each time an instance of <code>SocialPlatform</code> was created. I tought this variable was shared between all the classes that inherit from <code>SocialPlatform</code></p>

<p>So I tested that in my main function :</p>

<pre><code>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
</code></pre>

<p>but when I use this code I get this output :</p>

<pre><code>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
</code></pre>

<p>With this method <code>nb_post</code> is shared between Twitter, Facebook or Instagram instance, not all of them.</p>

<p>So my question is : is there any way to do this in python ?</p>

<p>You have to explicitly reference the base class in the increment expression:</p>

<pre><code>def _init_id_post(cls):
    cls.nb_post += 1
    return cls.nb_post
</code></pre>

<p>Should be:</p>

<pre><code>def _init_id_post(cls):
    SocialPlatform.nb_post += 1
    return SocialPlatform.nb_post
</code></pre>

<p>As per:</p>

<p><a href="http://stackoverflow.com/questions/25590649/how-to-count-the-number-of-instance-of-a-custom-class">How to count the number of instance of a custom class?</a></p>

python inheritance