Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NULP oop introduction

Yura Hulpa
February 23, 2016

NULP oop introduction

NULP oop introduction

Yura Hulpa

February 23, 2016

Transcript

20 slides
  1. “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
  2. Instance class WarLord(object): """warlord class”"" # Methods, attributes goes here

    # lord is an instance of WarLord class lord = WarLord()
  3. 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
  4. 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.
  5. “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
  6. 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)
  7. “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
  8. 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'
  9. “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
  10. 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)

More Decks by Yura Hulpa

Other Decks in Programming