Prev: Variables and Dynamic typing | Next: -
- Properties are variables (values) relevant to the state of the object
- Methods are routines (algorithms); when executed they have an effect upon and change the properties of the object
- A metaphor: imagine the object as a moving car. Its speed is a property of the car and it has some value at any specific moment. Stepping on the gas or pressing the brake are methods of the car that help the driver changing the speed.
- The metaphor continued: if a car is an object then the class is the design pattern of all cars of the same category (type). The constructor is the entire sequence of actions required so that the factory constructs a car object out of the class design pattern.
x = 1
the constructor of integer class is enacted and constructs an object somewhere in memory whith value '1' and methods inherited from the integer class.Generalizing: whatever we write on the right part of an assignment activates a constructor and constructs an object somewhere in memory.
x = 5 # constructs an integer
k = int(10) # an alternative way to construct an integer; int() is integer class constructor
y = 7.5 # constructs a float
z = random.randint(1,10) # constructs randomly an integer in [1,10]
a = Alpha() # constructs an object of the class 'Alpha'
x = 1
y = 15.7
z = 'spam'
print(type(x), type(y), type(z))
x = 1
y = 15.7
z = 'spam'
print(id(x), id(y), id(z))
x = 1
k = 2
y = 15.7
z = 'spam'
print(dir(x),'\n')
asum = x.__add__(k)
print(asum)
. Free learning material
. See full copyright and disclaimer notice