# Misunderstanding Python Class Attributes

DevFeed: [Misunderstanding Python Class Attributes](<https://devfeed.tech/articles/misunderstanding-python-class-attributes-32235.md>)

Original publisher: [Read original article](<https://bruceeckel.com/2022/05/11/misunderstanding-python-class-attributes/>)

Author: Bruce Eckel

Published: 2022-05-11T00:00:00Z

Content type: article

Language: en

Sources: [Bruce Eckel - Computing Thoughts](<https://devfeed.tech/sources/bruce-eckel-computing-thoughts.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [class](<https://devfeed.tech/topics/class.md>), [constructor](<https://devfeed.tech/topics/constructor.md>), [Open Source](<https://devfeed.tech/topics/open-source.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>)

Tags: [arguments](<https://devfeed.tech/tags/arguments.md>), [bugs](<https://devfeed.tech/tags/bugs.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [class](<https://devfeed.tech/tags/class.md>), [classes](<https://devfeed.tech/tags/classes.md>), [code](<https://devfeed.tech/tags/code.md>), [code-generation](<https://devfeed.tech/tags/code-generation.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [configuration](<https://devfeed.tech/tags/configuration.md>), [constructor](<https://devfeed.tech/tags/constructor.md>), [debugger](<https://devfeed.tech/tags/debugger.md>), [django](<https://devfeed.tech/tags/django.md>), [factory](<https://devfeed.tech/tags/factory.md>), [none](<https://devfeed.tech/tags/none.md>), [object](<https://devfeed.tech/tags/object.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [python](<https://devfeed.tech/tags/python.md>), [values](<https://devfeed.tech/tags/values.md>), [variables](<https://devfeed.tech/tags/variables.md>)

## AI overview

Python class attributes are shared bindings on the class, not per-instance defaults. Attribute lookup can make one appear to be a default: an instance with no matching attribute reads the class value, but assigning to that instance creates a separate value that shadows it. Changing the class attribute then affects instances that still rely on it, while instances with their own binding remain unchanged, which can produce surprising mixed behavior. The article recommends initializing per-instance values in a constructor with default arguments or using a dataclass, which generates a constructor from the declared fields. Class attributes also serve deliberately as templates in frameworks and code-generation patterns.

## Source excerpt

I was attempting to assist on an open-source project when I was stopped short by this (names have been changed): class DataPoint: measurement1 = None measurement2 = None measurement3 = None DataPoint was later used like this: d = DataPoint() d.measurement1 = 100 d.measurement2 = 200 d.measurement3 = 300 Why give names and initialization values to class attributes, then when you make an object, immediately create and initialize instance variables with the same names as the class attributes?