Skip to content

Instantly share code, notes, and snippets.

@gergob
gergob / oop_multi.py
Created December 8, 2014 05:42
multiple_inheritance_manual_constructor
class Phone(object):
def __init__(self):
print("Phone constructor invoked.")
def call_number(self, phone_number):
print("Calling number {}".format(phone_number))
@gergob
gergob / multiple_inheritance_in_action.py
Created November 26, 2014 20:58
Multiple Inheritance SmartPhone class in action
#
# will be discussed later why only the constructor of Phone class was invoked
#
>>> my_iphone = SmartPhone()
Phone constructor invoked.
>>> my_iphone.call_number("123456789")
Calling number 123456789
>>> my_iphone.install_software("python")
@gergob
gergob / multiple_inheritance.py
Created November 26, 2014 20:13
Phone, Computer, SmartPhone classes implemented using multiple inheritance in Python 3.x
class Phone:
def __init__(self):
print("Phone constructor invoked.")
def call_number(self, phone_number):
print("Calling number {}".format(phone_number))
@gergob
gergob / snake.py
Created November 26, 2014 19:25
Snake class implementation in Python 3.x
class Snake(Animal):
__temperature = 28
def __init__(self, name, temperature):
super().__init__(name, 2, True, 0)
self.temperature = temperature
#
# METHODS
#
@gergob
gergob / animal.py
Created November 26, 2014 19:22
Animal class implemented in Python 3.x
class Animal:
__name = None
__age = 0
__is_hungry = False
__nr_of_legs = 0
def __init__(self, name, age, is_hungry, nr_of_legs):
self.name = name
self.age = age
self.is_hungry = is_hungry
@gergob
gergob / p_gadget_console.txt
Created November 21, 2014 21:25
using Gadget python class
>>> from Gadget import Gadget
>>> my_iphone = Gadget(240,'iOS',1980,4)
>>> my_iphone.weight
240
>>> my_iphone.weight = 255
>>> my_iphone.weight
255
>>>
>>>
>>> my_iphone.operating_system
class Gadget:
"""A class used for modelling Gadgets in a web shop."""
__weight = 100
__operating_system = None
__battery_capacity = 2000
__screen_size = 1
def __init__(self, weight, operating_system, battery_capacity, screen_size):
self.__weight = weight
self.__operating_system = operating_system
@gergob
gergob / Gadget3.py
Created November 21, 2014 20:52
Gadget without respecting OOP encapsulation
my_iphone = Gadget(weight = 128, operating_system="iOS", battery_capacity=2800, screen_size=4)
print(my_iphone.weight)
print(my_iphone.operating_system)
print(my_iphone.battery_capacity)
print(my_iphone.screen_size)
@gergob
gergob / Gadget2.py
Created November 21, 2014 20:30
Gadget class with constructor
class Gadget:
weight = 100
operating_system = None
battery_capacity = 2000
screen_size = 1
def __init__(self, weight, operating_system, battery_capacity, screen_size):
self.weight = weight
self.operating_system = operating_system
self.battery_capacity = battery_capacity
@gergob
gergob / Gadget.py
Last active August 29, 2015 14:10
Gadget python class
class Gadget:
weight = 100
operating_system = None
battery_capacity = 2000
screen_size = 1
my_iphone = Gadget()