June 19, 2018

Python class examples

<Code1.py>
class Car(object):
    __w = 4
    __d= 2

    def __init__(self, v1, v2):
        print "__init__() is called"
        self.v1 = v1
        self.v2 = v2

    def func1(self):
        print "w_ => {}".format(self.__w)
        print "d_ => {}".format(self.__d)
   
    def __del__(self):
        print "__del__() is called"


# Instance object
car_object = Car(2, 3)
car_object2 = Car(2, 3)

print "address(Car) => {0:#x}".format(id(Car))
print "address(Car_object) => {0:#x}".format(id(car_object))


print id(car_object.v1), id(car_object2.v1)


car_object.func1()


if (isinstance(car_object, Car) == True):
    print "yes"
else:
    print "no"

class_of_car_object = car_object.__class__
class_of_car_ojbect2 = car_object2.__class__

print class_of_car_object, class_of_car_ojbect2

print car_object2.__w


<Code1.py result>
__init__() is called
__init__() is called
address(Car) => 0x60014f4f0
address(Car_object) => 0x6ffffe60d90
25770367776 25770367776
w_ => 4
d_ => 2
yes
<class '__main__.Car'> <class '__main__.Car'>
Traceback (most recent call last):
  File "C:\Users\user\Desktop\tmp\test.py", line 43, in <module>
    print car_object2.__w
AttributeError: 'Car' object has no attribute '__w'
__del__() is called
__del__() is called

❑ [Object].__class__ : Returns the class to which the [Object] belongs.
❑ Error occurs because __w is a variable that is set to private by the underscore(__).
* [Information hiding]
__name == private
_name == protected
name == public
❑ The constructor and The Destructor are called automatically at the beginning and end.




<Code2.py>
class P:
  v=100

class C(P): #Inheritance
  pass

class Np:
vv=15
__w = "This is __w"
_wp = "This is _wp"

def __f(self):
print "This is private method"

def wFunc(self):
print self.__w


objectC = C()
print objectC.v

print issubclass(C, P)
print issubclass(C, C)
print issubclass(C, Np)
print issubclass(C, object) #True in Python 3.x

print C.__bases__
print P.__bases__
print Np.__bases__ # An Object is displayed in Python 3.x



class CC(P, Np): # Multiple inheritance
pass

objectCC = CC()

print objectCC.vv
# print CC.__w
# print CC.__f() # The private member variables/functions are not inherited.
print objectCC._wp
objectCC.wFunc()

<Code2.py result>
100
True
True
False
False
(<class __main__.P at 0x6ffffe56598>,)
()
()
15
This is _wp
This is __w

❑ A class's subclass includes itself.
❑ In Python 3.x, the object class is the parent class of all classes.
❑ [Class].__bases__ : Check the parent class of the [Class].
❑ Private member variables/functions are not inherited, but protected member variables/functions are inherited.
❑ The protected member variable/function can only be called in inheritance.




<Code3.py>
class Pp:
    v = 0x12
 
class C2(Pp):
    pass
 
objectC2= C2()
objectC2_2= C2()
objectC2.w = 10 
#The new member variable "w" is created in the objectC2.
print objectC2.w

print hex(id(objectC2.v))
print hex(id(objectC2_2.v))
print hex(id(C2.v))

objectC2.v = 55
print hex(id(objectC2.v))
print hex(id(objectC2_2.v))
print hex(id(C2.v))

C2.kVal = 150
print objectC2.kVal
print objectC2_2.kVal



class P10(object):
    def f(self):
        print "Parent f()"
     
class P11(P10):
    def f(self):    #Redefine
        print "Child f()"
    pass
 
objectP11 = P11()
objectP11.f()

<Code3.py result>
10
0x6000899a0
0x6000899a0
0x6000899a0
0x600089df0
0x6000899a0
0x6000899a0
150
150
Child f()

❑ If a member variable/function is redefined, the variable/function of the child object is used.