Home      |       Contents       |       About

Prev: Variables and Dynamic typing      |      Next: -

Where do types come from?

  • "Types come from classes". To understand this we need to talk a bit about objects and classes in the Python class hierarchy.

(a) Python class hierarchy

  • Although Python does not impose OOP (Object-Oriented Programming) on programmers still "everything in Python is an object!" and this is a statement that you should scratch a little to develop a better understanding of how things work.

What is an object?

  • For those not familiar with OOP it's enough to say that an object in OOP is a specific form of data structure that possesses properties and methods.
    • 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.

And what is a class?

  • A class in OOP is a proper description of the properties and methods of a category (a class) of objects. Thus a class is like an object pattern describing the architecture of the objects to be constructed.
  • Each class includes a specific 'constructor' method which is executed when the programmer decides that an object of this class is needed.
    • 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.

Classes and objects in Python

  • Each time an assignment command is executed a new object is constructed in memory. To construct this object the constructor of the appropriate class is enacted while executing the code.
  • Thus, when we write 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'

Class hierarchy

  • Python coding is based on a hierarchy of classes that produce the various data types available to programmers.
  • Typically all Python classes are considered as subclasses of a base class called 'object' as the graph below depicts.
  • With each class you write you actually enrich and extend this hierarchy but this is an issue to be discussed in the OOP section.

type(), id(), dir()

  • Let's get more practical now and see three very helpful methods to identify classes and objects

Use type() to get the type (class) of an object

In [6]:
x = 1 
y = 15.7
z = 'spam'
print(type(x), type(y), type(z))
<class 'int'> <class 'float'> <class 'str'>

Use id() to get the unique integer identifier of an object

  • In CPython this integer is the memory adress of the object
In [5]:
x = 1 
y = 15.7
z = 'spam'
print(id(x), id(y), id(z))
1651830544 56813968 68799152

Use dir() to get the list of attributes (methods and properties) an object

  • See how we can call the method 'add' to alternatively implement x+k addition (but don't worry about that right now; it's a more advanced issue and will be discussed in OOP section).
In [7]:
x = 1 
k = 2
y = 15.7
z = 'spam'
print(dir(x),'\n')

asum = x.__add__(k)
print(asum)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes'] 

3

. Free learning material
. See full copyright and disclaimer notice