12.3. OOP Attribute About¶
Attributes are also known as "Properties" or "Fields"
Attributes store information for instances
Access field values using dot (
.
) notation
- attribute¶
Variable inside the class. In Python, methods also can be described as attributes, but justification for that is a bit more complex which will be introduced later in a book.
- field¶
Variable inside the class. Can be used as a synonym of property or state.
- property¶
Variable inside the class. Should not change during lifetime of an object.
- state¶
Variable inside the class. Changes during lifetime of an object. Represents current state of an object.
- namespace¶
Container for storing related data
12.3.1. About¶
Class example with distinction of properties and state attributes.
An example "Glass with Water" can illustrate the distinction of properties and state attributes:
Properties:
color
width
height
radius
capacity
net mass (without water)
State:
volume (how much water is currently in the glass)
gross mass = net mass + water mass (water mass depends on its volume used))

12.3.2. What are attributes?¶
Scalars creates values
Identifiers and values creates variables
Values with relations creates structures
Structures with identifiers creates data
Data with context and relations creates information
Scalars creates values:
>>> 1
>>> 2
>>> 3
Identifiers and values creates variables:
>>> x = 1
>>> y = 2
>>> z = 3
Values with relations creates structures:
>>> point = [1, 2, 3]
Structures with identifiers creates data:
>>> point = {
... 'x': 1,
... 'y': 2,
... 'z': 3,
... }
Data with context and relations creates information:
>>> class Point:
... x: int = 1
... y: int = 2
... z: int = 3
12.3.3. Namespace¶
Class creates space, in which names has meaning
Unrelated variables:
>>> x: int
>>> y: int
>>> z: int
Class creates space, in which names has meaning:
>>> class Point:
... x: int
... y: int
... z: int