Share
Embed
Transcript
20 slides
-
“Object-oriented programming (OOP) is a programming paradigm based on the
concept of "objects", which are data structures that contain data, in the form of fields, o en known as a ributes; and code, in the form of procedures, o en known as methods.” Wikipedia Definition -
Instance class WarLord(object): """warlord class”"" # Methods, attributes goes here
# lord is an instance of WarLord class lord = WarLord() -
Magic methods __init__ class ClassName(object): """warlord class""" def __init__(self, name):
super(ClassName, self).__init__() self.name = name -
__init__ Use __init__ when you need to control initialization of
a new instance. -
Magic methods __new__ class WarlordClass(object): """warlord class""" instance = None
def __new__(cls, name): if cls.instance is None: cls.instance = super(WarlordClass, cls) .__new__(cls) return cls.instance -
__new__ Use __new__ when you need to control the creation
of a new instance -
More theory __new__ is the first step of instance creation.
It's called first, and is responsible for returning a new instance of your class. In contrast, __init__ doesn't return anything; it's only responsible for initializing the instance a er it's been created. -
“In object-oriented programming, inheritance is when an object or class
is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original so ware via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. Inheritance was invented in 1967 for Simula.” Wikipedia Inheritance -
class PLayerCharacter(object): def get_weapon(self): print 'I am trying, sir!' def
war_song(self): print 'With what?' class Orc(PLayerCharacter): def get_weapon(self): print 'My Axe is ready' def war_song(self): print "For the Sauron!" class Uruk(PLayerCharacter): def get_weapon(self): print 'Broad-bladed swords are ready!' def war_song(self): print 'For the Saruman!' class MutantOrc(Orc, Uruk): pass class TestPlayers(object): def get_character_weapon(self, character): character.get_weapon() def test_character_song(self, character): character.war_song() fantasy_test = TestPlayers() orc = Orc() uruk = Uruk() fantasy_test.test_character_song(orc) fantasy_test.test_character_song(uruk) fantasy_test.get_character_weapon(orc) fantasy_test.get_character_weapon(uruk) -
“In programming languages, encapsulation is used to refer to one
of two related but distinct notions, and sometimes to the combination thereof: A language mechanism for restricting access to some of the object's components. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data” Wikipedia Encapsulation -
class PLayerCharacter(object): def __get_weapon(self): print 'I am hidden secret weapon'
def get_weapon(self): print 'My sword is ready!!!' player = PLayerCharacter() player.__get_weapon() Traceback (most recent call last): File "incapsulating.py", line 14, in <module> player.__get_weapon() AttributeError: 'PLayerCharacter' object has no attribute '__get_weapon' # But you can call it like this: player._PLayerCharacter__get_weapon() ==> 'I am hidden secret weapon' -
“In programming languages and type theory, polymorphism is the provision
of a single interface to entities of different types. A polymorphic type is one whose operations can also be applied to values of some other type, or types” Wikipedia Polymorphism -
class PLayerCharacter(object): def get_weapon(self): pass def attack(self): pass class OrcWarior(PLayerCharacter):
def get_weapon(self): print 'My Axe is ready' def attack(self): print '(Orc is atacking you with Axe)' class ElfArcher(PLayerCharacter): def get_weapon(self): print 'Arrows are ready!' def attack(self): print '(Elf is shooting you with arrow)' class TestCharacter(object): def get_character_weapon(self, character): character.get_weapon() def test_character_attack(self, character): character.attack() fantasy_test = TestCharacter() orc = OrcWarior() elf = ElfArcher() fantasy_test.get_character_weapon(orc) fantasy_test.get_character_weapon(elf) fantasy_test.test_character_attack(orc) fantasy_test.test_character_attack(elf)