Thursday, June 22, 2017

Summer of code 2017: Python, Day 5 Objects


As explained in my Summer of code 2017: Python post I decided to pick up Python

Today is officially day 5, today, I looked at objects in Python, here are my notes

if you do something like this

x = 1000

Python creates an int object with a value of 1000, x now points to this object with a value of 1000
if you now do this
x = 500

Python will not change the value of the object from 1000 to 500 but will instead create another object with a value of 500 and z will now point to that object. The garbage collector will destroy the object with a value of 1000. This all happens because in Python objects are immutable, they cannot change


If you assign  1000 to x, then assign 500 to y, then assign x to y, both are pointing to the same object. You can prove that by using the id() function

In this example below, you will see that after we assign x to y, bot x and y point to the same object with id 50127440


>>> x = 1000
>>> y = 500
>>> y =x
>>> y
1000
>>> x
1000
>>> id(x)
50127440
>>> id(y)
50127440
>>> 


What if you have two variables and assign the same value?

>>> a = 1
>>> b =1
>>> id(a)
499688544
>>> id(b)
499688544
>>> 


As you can see, those also point to the same object

You can also us is to see if 2 objects are the same, so if you do x is y, you will get back True or False


>>> x = 5
>>> y = 5
>>> x is y
True
>>> 



If you do math against a variable, the id will also change

>>> z = 5
>>> id(z)
499688672
>>> z +=2
>>> id(z)
499688736
>>> 



With list it works like this

if you have a list d [1, 2, 3], now you add list e with the values of list d, now both d and e are [1, 2, 3]. If you now modify the list for e to be [1, 1000, 3],  both  e and d are now [1, 1000, 3]. This is because you modified the list object from [1, 2, 3] to [1, 1000, 3] and both d and e point to this list object

Here is what it looks like in IDLE

>>> d = [1, 2, 3]
>>> d
[1, 2, 3]
>>> e = d
>>> e
[1, 2, 3]
>>> e[1] = 1000
>>> e
[1, 1000, 3]
>>> d
[1, 1000, 3]
>>> e is d
True
>>> 


What will happen if we create 2 lists with the same values by assign the values to each list. Now the values are the same

>>> f = [1, 2]
>>> g = [1, 2]
>>> f == g
True
>>> f is g
False
>>> f == f
True


With integers, both is and  == return true

>>> x = 1
>>> y = 1
>>> x == y
True
>>> x  is y
True
>>> 


The is operator determines equality of identity, == determines equivalence


So even though people call these object variables in Python, these are really named references to objects


I also learned about the dir built-in function

Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:


  • If the object is a module object, the list contains the names of the module’s attributes.
  • If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
  • Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.


So for example if we call dir on the math object, it will return all the functions available

>>> import math
>>> type(math)

>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh',
 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 
'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 
'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 
'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 
'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> math.pi
3.141592653589793
>>> math.pow(2,4)
16.0
>>> 

So this is a quick way to get all the available methods/functions from a class

No comments: