Python object-oriented programming

Python object-oriented programming

Learn how to create a class in Python with properties and methods as well as instantiating instances of those classes


In programming, classes are used to define a basic structure that can be used to create many instances of itself called objects, each object created from a class functions independently from one-another.


A class generally contains several attributes and methods.


An attribute is a variable within a class that can either be shared across all objects or more commonly separated from other objects in that they can hold different values from one-another. If we created a class called "person" then some of the attributes within that class could be "age", "name" and "surname", each of which would have a different value when created as an object.


A method is a function that is defined within a class. These can be used by objects created from that class. For the "person" class, we could have included methods such as "walk" and "talk" which every object created from that class could use.


Defining a class in Python


In Python, a class can be created using the

class

keyword followed by the name you wish to give that class, the attributes and methods of that class will reside inside a code block so we must indent properly and use a colon ":".


class classname:"documentation"//attributes and methods

Within the class we can define a string to be used for documentation purposes, this would usually describe the class, it can be accessed using "

classname

.__doc__" , this is optional and can be omitted entirely. After the documentation string you can define your attributes and methods.


Creating instances of a class (objects)


When calling a class to create an object, Python will look for a constructor within that class which is a method that will be called every time an instance of the class is created. The constructor method has a special name called "__init__", this can be used to provide arguments that can be used within the new objects and set attributes.


class classname:    
     
 def __init__(self, arg1, arg2):    
  //set attributes and other code

As we can see above, the __init__ method (as well as any other methods that you include in your class) must contain the self parameter which is used to access the attributes and methods of the current object being used, when calling a function we do not need to provide a value for it as Python does this automatically.


Once we've added a constructor to our class, we can then call the class to create an object by referencing the class name, followed by a list of arguments, we then assign this to a variable to hold the created object.


object = classname(arg1, arg2)


Class attributes


As explained above, an attribute is a variable within the class which can either be shared by all instances of the class or only used by a single object.


To create a

shared attribute

, we must define it outside of the class's functions.


class classname:    
     
 sharedVariable = value    
    
 def __init__(self, arg1, arg2):    
  //set attributes and other code

To access this kind of variable we can use:


classname.sharedVariable

This can be used to access the shared variable both inside and outside the class


For

attributes that are unique to each object

, we must include them within any method in the class preceded by the self keyword.


class classname:    
     
 def __init__(self, arg1, arg2):    
  self.uniqueVariable = value

This attribute can only be accessed once an object has been created from the class like so:


object = classname(arg1, arg2)    
    
object.uniqueVariable

It can also be accessed from within the class using the self keyword again, but only within the class's methods.


class classname:    
     
 def __init__(self, arg1, arg2):    
  self.uniqueVariable = value  
   
 def exampleMethod( self ):  
  print( self.uniqueVariable )


Class methods


A method is a function that resides within a class. These are used to provide functionality to classes.


A method is defined similarly to regular functions, except self must be added as a parameter.


class classname:  
 //constructor method and attributes  
  
 def methodName(self, args):  
  //function code  
  


A method defined within a class can be called after creating an object like so:


object = classname()    
    
object.methodName()

As with the constructor, you do not need to provide a value for the self parameter as Python does this automatically.


Creating a class with attributes, methods and a constructor (example)


For this example, let's create a new class called "Person" that will have the following attributes unique to each object:


- name


- surname


- age


And the shared attribute "people" which will hold the number of Person objects that have been created.


The "Person" class will also have the following methods:


- printInfo : this will print the name, surname and age of a Person object.


- birthday : this will increment the age of a Person object and then call the printInfo method.


class Person:  
   
 people = 0   
  
 def __init__( self, name, surname, age ):  
  self.name = name  
  self.surname = surname  
  self.age = age  
  Person.people += 1  
  
 def printInfo( self ):  
  print( self.name + " " + self.surname + " is " + self.age + " years old" )  
  
 def birthday( self ):  
  self.age += 1  
  self.printInfo()  
  
person1 = Person( "john", "tyler", 24 )  
person2 = Person( "cindy", "oliver", 43 )  
  
print( Person.people ) // prints "2"  
print( person1.name ) // prints "john"  
print( person2.age ) // prints "43"  
  
person2.printInfo() // prints "cindy oliver is 43 years old"  
person2.birthday() // prints "cindy oliver is 44 years old"



Christopher Thornton@Instructobit 7 years ago
or