|
| 1 | +#Shared variables |
| 2 | + |
| 3 | +''' |
| 4 | +What we need is a way to make an attribute shared across objects. |
| 5 | +The data is shared by all objects, not owned by each object. |
| 6 | +Thus, by making a single change, it should reflect in all objects at one go. |
| 7 | +''' |
| 8 | + |
| 9 | +#Static |
| 10 | + |
| 11 | +''' |
| 12 | +We can create shared attributes by placing them directly inside the class and not inside the constructor. |
| 13 | +And since this attribute is not owned by any one object, we don’t need the self to create this attribute. |
| 14 | +Such variables which are created at a class level are called static variables. |
| 15 | +Here discount is a static value. |
| 16 | +''' |
| 17 | + |
| 18 | + |
| 19 | +class Mobile: |
| 20 | + discount = 50 |
| 21 | + def __init__(self, price, brand): |
| 22 | + self.price = price |
| 23 | + self.brand = brand |
0 commit comments