{"id":89,"date":"2011-01-26T17:53:00","date_gmt":"2011-01-26T17:53:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=89"},"modified":"2011-01-26T17:53:00","modified_gmt":"2011-01-26T17:53:00","slug":"python-class-attributes-versus-instance-attributes","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=89","title":{"rendered":"Python class attributes versus instance attributes"},"content":{"rendered":"<p>Today I finally found out the difference between <a href=\"http:\/\/docs.python.org\/tutorial\/classes.html#class-objects\">class<\/a> and <a href=\"http:\/\/docs.python.org\/tutorial\/classes.html#instance-objects\">instance<\/a> attributes in Python. In C++, this is done by putting the <span style=\"font-family: 'Courier New', Courier, monospace;\">static<\/span> modifier in front of the declaration. Consider the following code:<\/p>\n<pre>#!\/usr\/bin\/env python<br \/><br \/>class B:<br \/>    b = 2<br \/><br \/>class C:<br \/>    a = B()<br \/><br \/>    def __init__(self):<br \/>        self.a.b = 1<br \/><br \/>c = C()<br \/>c.a.b = 3<br \/>b = C()<br \/><br \/>print c.a.b, b.a.b<br \/><\/pre>\n<p>Here, <span style=\"font-family: 'Courier New', Courier, monospace;\">a<\/span> is a class attribute of class <span style=\"font-family: 'Courier New', Courier, monospace;\">C<\/span>. That is, there exists only one such attribute for all objects of kind <span style=\"font-family: 'Courier New', Courier, monospace;\">C<\/span>. So the output of the print statement will be &#8220;1 1&#8221;. This is the same as a <span style=\"font-family: 'Courier New', Courier, monospace;\">static<\/span> attribute in C++. But often I want an instance attribute. The correct way to do this would have been:<\/p>\n<pre>#!\/usr\/bin\/env python<br \/><br \/>class B:<br \/>    b = 2<br \/><br \/>class C:<br \/>    def __init__(self):<br \/>        self.a = B()<br \/>        self.a.b = 1<br \/><br \/>c = C()<br \/>c.a.b = 3<br \/>b = C()<br \/><br \/>print c.a.b, b.a.b<br \/><\/pre>\n<p>Now the output is &#8220;3 1&#8221;, just as expected. I guess this all somehow makes sense in the Python world, but I tripped over this, and worst of all: Sometimes you don&#8217;t even notice. If the class attribute is a simple type, like <span style=\"font-family: 'Courier New', Courier, monospace;\">int<\/span>, the first solution would have worked. However, I have not yet understood why that is the case. One more Python semantic that eluded me so far.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I finally found out the difference between class and instance attributes in Python. In C++, this is done by putting the static modifier in front of the declaration. Consider the following code: #!\/usr\/bin\/env pythonclass B: b = 2class C: a = B() def __init__(self): self.a.b = 1c = C()c.a.b = 3b = C()print c.a.b, &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=89\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python class attributes versus instance attributes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/89"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}