OO (Python)
Versie door Jeroen Strompf (overleg | bijdragen) op 18 jan 2019 om 15:44 (→Delete a member - Dictionary)
Python is een object oriented programmeertaal. Hoe werkt dat ook al weer?
Voorbeeld: Zelfs een string is een object
>>> mijnobject = "hallo >>> print mijnobject hallo >>> print 'mijnobject'.upper() MIJNOBJECT
Klasse
- De class of klasse van een object, is de definitie, blauwdruk, sjabloon of template om nieuwe exemplaren van dat object te instantiëren. De klasse-aanroep geschiedt hier impliciet - Simpelweg door de string te instantiëren
- Algemener: Een klasse is de defintie van een verzameling samenhangende gegevens en functies [1].
Attributen
upper()
is een attribuut oftewel eigenschap van deze klasse/dit object. Attributen roep je aan met de dot-operator. Hier kun je ziet dat dit echt een attribuut betreft, en geen method:
>>> print 'myobject'.mijnfantasieding() AttributeError: 'str' object has no attribute 'mijnfantasieding'
Mijn eerste klasse
#! /usr/bin/python3 # ############################################################################## class Foo: "Here is an explanation about the foo class" def helloworld(self): # A method is a function within a class print ("Hello, world! - Greetings from foo") def bye_world(self): print ("Bye, world! - Greetings from foo") def setx(self, x): # x is a member self.x =x def printx(self): print (self.x) def reset_x(self): self.x=0 def inc_x(self): self.x=self.x+1 ############################################################################## # Example 1 - All worked as expected ############################################################################## # # f=Foo() # f.helloworld() # f.bye_world() # f.setx(12) # f.printx() # f.reset_x() # f.printx() # f.inc_x() # f.printx() # f.inc_x() # f.printx() ############################################################################## # You can't print an object that isn't initiated yet: ############################################################################## # # Error: 'Foo' object has no attribute 'x' ########################################## # # f=Foo() # f.printx() # OK ########################################## # f=Foo() f.reset_x() f.printx()
Klasses, Init & variabelen
Voorbeeld van initialization of init:
#! /usr/bin/python3 # ############################################################################## class Foo: "Here is an explanation about the foo class" def __init__(self): print("The world is an instance of Foo richer!") self.x=0 # Init var x + set to 0 ############################################################################## # __init__ ############################################################################## # f=Foo() print(f.x)
Output:
The world is an instance of Foo richer! 0
Als de regel met self.x=0
wordt uit-gecommentariseerd, krijg je als output:
The world is an instance of Foo richer! Traceback (most recent call last): File "./klasse-03.py", line 36, in <module> print(f.x) AttributeError: 'Foo' object has no attribute 'x'
Interessant: Het script wordt wel uitgevoerd tot aan het probleem.
Delete a member - Dictionary
#! /usr/bin/python3 # ############################################################################## class Foo: "Here is an explanation about the foo class" def helloworld(self): print ("Hello, world! - Greetings from foo") def bye_world(self): print ("Bye, world! - Greetings from foo") def setx(self, x): self.x =x def printx(self): print (self.x) def reset_x(self): self.x=0 def inc_x(self): self.x=self.x+1 ############################################################################## # Delete a member ############################################################################## # # Je kunt een member (=een variabele van een object) verwijderen. Deze code # geeft daarom foutmelding AttributeError: 'Foo' object has no attribute 'x' # # f=Foo() # f.reset_x() # del f.x # f.printx() ############################################################################## # Dictionary ############################################################################## # f=Foo() f.reset_x() f.inc_x() f.printx() vars(f) # Gebeurt nix #f.vars() # No such attribute
Ik ben tot hiero gekomen
https://en.wikibooks.org/wiki/Python_Programming/Classes#Dynamic_Class_Structure
Bronnen
- https://en.wikibooks.org/wiki/Python_Programming/Basic_Syntax - Voorbeeld van een string als object
- https://en.wikibooks.org/wiki/Python_Programming/Classes