OO (Python): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 24: Regel 24:
 
>>> print 'myobject'.mijnfantasieding()
 
>>> print 'myobject'.mijnfantasieding()
 
AttributeError: 'str' object has no attribute 'mijnfantasieding'
 
AttributeError: 'str' object has no attribute 'mijnfantasieding'
 +
</pre>
 +
 +
== Mijn eerste klasse ==
 +
 +
<pre>
 +
#! /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
 +
 +
 +
##############################################################################
 +
# 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()
 
</pre>
 
</pre>
  

Versie van 11 jan 2019 12:37

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):
    	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


##############################################################################
# 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()

Bronnen