Python:Classes: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Python]]
Create your own classes
Create your own classes


Line 5: Line 6:


;__init__(self, <parameters that can be provided with default value>)
;__init__(self, <parameters that can be provided with default value>)
:Constructor, automatically called when the object is instantiated.
:Constructor, automatically called when the object is instantiated. Use this to initialize the object attributes. An object is instantiated by just assigning a class to a variable. <code>a = class()</code>


;__del__(self)
;__del__(self)
:Destructor, automatically called when the object is removed.
:Destructor, automatically called when the object is removed.


;__str__(self)
;__str__(self)
:Returns the string representation of the object. E.g. if you use print(object) or str(object)  
:Returns the string representation of the object. E.g. if you use print(object) or str(object)  


All interaction with the object should go via a method, not to variables in the class.
All interaction with the object should go via a method, not to variables in the class (use interface, not implementation). Python has no mechanism to enforce this. Variable name can be obfuscated by putting underscore before their name. You can do this with methods too that should not be called directly but only from other methods.


Example:
Example:
<syntaxhighlight lang=python>
<syntaxhighlight lang=python>
class Medium:
class Medium:
     def __init__(self, titel='', prijs=0):
     def __init__(self, title='', price=0):
         self.titel = titel
         self.__title = title
         self.prijs = prijs
         self.__price = price
      
      
     def __str__(self):
     def __str__(self):
         return "Titel: {0}\nPrijs: {1:6.2f}".format(self.titel,self.prijs)
         return "Title: {0}\nPrice: {1:6.2f}".format(self.__title,self.__price)


     def gettitel(self):
     def gettitle(self):
         return self.titel
         return self.__title


     def settitel(self, titel):
     def settitle(self, title):
         self.titel = titel
         self.__title = title


     def getprijs(self):
     def getprice(self):
         return self.prijs
         return self.__price


     def setprijs(self,prijs):
     def setprijs(self,price):
         self.prijs = prijs
         self.__price = price


     def getall(self):
     def getall(self):
         return [self.gettitel(),self.getprijs()]
         return [self.gettitle(),self.getprice()]
</syntaxhighlight>
</syntaxhighlight>
;class(super)
:Create a sub-class for the super-class.
;super().<method>
:Call the method from the super-class.

Latest revision as of 13:54, 9 December 2018

Create your own classes

self
The namespace of the current instance. Can have any name, 'self' is commonly used.
__init__(self, <parameters that can be provided with default value>)
Constructor, automatically called when the object is instantiated. Use this to initialize the object attributes. An object is instantiated by just assigning a class to a variable. a = class()
__del__(self)
Destructor, automatically called when the object is removed.
__str__(self)
Returns the string representation of the object. E.g. if you use print(object) or str(object)

All interaction with the object should go via a method, not to variables in the class (use interface, not implementation). Python has no mechanism to enforce this. Variable name can be obfuscated by putting underscore before their name. You can do this with methods too that should not be called directly but only from other methods.

Example:

class Medium:
    def __init__(self, title='', price=0):
        self.__title = title
        self.__price = price
    
    def __str__(self):
        return "Title: {0}\nPrice: {1:6.2f}".format(self.__title,self.__price)

    def gettitle(self):
        return self.__title

    def settitle(self, title):
        self.__title = title

    def getprice(self):
        return self.__price

    def setprijs(self,price):
        self.__price = price

    def getall(self):
        return [self.gettitle(),self.getprice()]
class(super)
Create a sub-class for the super-class.
super().<method>
Call the method from the super-class.