class

In the previous chapetr we defined a decorator, like:

In [ ]:
from take_time import timeit

Define a object

In [ ]:
class Rectangle(object):
    def __init__(self, s0, s1):
        self.s0 = s0
        self.s1 = s1
        
    @timeit
    def perimeter(self):
        return 2 * (self.s0 + self.s1)
    
    def area(self):
        pass
In [ ]:
square = Rectangle(5, 5)
square.perimeter()

Exercise 10

Complete the class writing the method to compute the area... [2 minutes]

In [ ]:
 

Exercise 11

Define a new class for a circle: [5 minutes]

In [ ]:
 

Inheritance

In [ ]:
class Person(object):
    def __init__(self, name, surname):
        self.name = name.capitalize()
        self.surname = surname.capitalize()
     
        
class Contact(Person):
    def __init__(self, address=None, mobile=None, phone=None, *args, **kargs):
        # super is a function that call the method of the parent object
        super(Contact, self).__init__(*args, **kargs)
        self.address = address
        self.mobile = mobile
        self.phone = phone
In [ ]:
pietro = Person('pietro', 'zambelli')
In [ ]:
pietro.name
In [ ]:
zambelli = Contact(name='pietro', surname='zambelli', address='via Mesiano 77', phone='0461282696')
In [ ]:
zambelli.phone
In [ ]:
zambelli.name
In [ ]:
zambelli
In [ ]:
print zambelli

Exercise 12

Define a new class that inherit from the Contact and return a beatiful output, definig the magic methods str, repr: [5 min]

In [ ]:
class NiceContact(Contact):
    def __init__(self):
        pass
    
    def __str__(self):
        pass
    
    def __repr__(self):
        pass

Below you can find a test:

In [ ]:
pietro = NiceContact(name='pietro', surname='zambelli', address='via Mesiano 77', phone='0461282696')
print repr(pietro)  # use __repr__
print pietro        # use __str__

Other magic methods

In [ ]:
one = 1
two = 2.
In [ ]:
one.__add__(2)
In [ ]:
two.__sub__(one)
In [ ]:
'a'.__add__('b') == 'a' + 'b'
In [ ]:
phone='0461282696'
phone.isdigit()
In [ ]:
class Contact(Person):
    def _get_phone(self):
        return self._phone
        
    def _set_phone(self, phone):
        if phone.isdigit():
            if len(phone)>8:
                self._phone = phone
            else:
                raise TypeError("Phone number must be longer than 8 characters")
        else:
            raise TypeError("Phone number must contain only numbers")
                            
    phone = property(fget=_get_phone, fset=_set_phone)
    
    @property
    def complete_name(self):
        return "%s %s" % (self.name, self.surname)
    
    def __init__(self, address=None, mobile=None, phone=None, *args, **kargs):
        # super is a function that call the method of the parent object
        super(Contact, self).__init__(*args, **kargs)
        self.address = address
        self.mobile = mobile
        self.phone = phone
In [ ]:
pietro = Contact(name='pietro', surname='zambelli', address='via Mesiano 77', phone='0461282696')
In [ ]:
pietro.phone
In [ ]:
pietro.phone = '045.'
In [ ]:
pietro.complete_name

Exercise 13

Using the string methods, modify the _set_phone, to manage and clean string with strange characthers [2 min]

In [ ]:
phone = '+39 046-128.26/96'  # cleaned: '390461282696'
In [ ]: