Presentation on theme: "OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie."— Presentation transcript:
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie
Python OOP – David Moodie OOP Vs. Procedural OOPProcedural Organised by class structuresDivided into ‘chunks’ e.g. functions and procedures / subroutines Steeper learning curveEasy to get in to Improves scalability (modularity)Code can become long and difficult to understand Information-hiding (further abstraction) Not so easily extensible Code-reuse (Less typing and cleaner) 2
Python OOP – David Moodie What is OOP? OOP is a new programming paradigm. This means it is a different ‘way’ of programming that introduces new terms, concepts and rules. We work with ‘classes’ and ‘objects’ in OOP. Examples of objects in python: strings, dictionaries, integers, functions. (Everything) 3
Python OOP – David Moodie Classes A class is basically like a ‘blueprint’ for objects. This ‘blueprint’ contains properties and methods. Example: The ‘str’ class is the ‘blueprint’ for all strings in python. Any text wrapped in quotes such as “Dave” becomes a string object. All the methods associated with strings (defined in the str class) are also ‘bound’ to this object and can be run on an object using ‘dot syntax’ such as “dave”.upper() which would return the string “DAVE”. 6
Python OOP – David Moodie We define methods in classes that will be useful for all objects created from the class (such as the str.upper() method which can be used on all objects of *type string. Properties are defined in classes and will be inherited by any objects that are created from this class. Every object can have different values for these properties but the property identifiers remain the same. For example a ‘person’ object may have a hairColour property of ‘brown’ whereas another could have a hairColour property of ‘blue’. They are both still ‘person’ objects but have different values for their properties. 7 Type: An object’s type in python is defined as the class that was used to create it. For example an Integer’s type is ‘int’; a string’s type is ‘str’.
Python OOP – David Moodie OOP Vs. Procedural Example 8 Procedural Version
Python OOP – David Moodie OOP Vs. Procedural Example 9 OOP Version
Python OOP – David Moodie Break it down This statement creates a new class called ‘name’ and inherits the properties of the class ‘object’ Inheriting is useful as it gives access to already defined methods and attributes for your class. The class you inherit from is called the ‘parent’ class. 10 Parent class Name of class
Python OOP – David Moodie There is a lot going on here. The ‘__init__’ method (two underscores prepended and appended) is called the ‘constructor’ method in most languages. It is called when you first *instantiate an object using your class. For example ‘name(“David Moodie”)’ would create an object of type ‘name’ and immediately run the ‘__init__’ method and pass the *argument “David Moodie” to it. I.e.: To pass an initialisation (setup) variable to a class you would do ‘myclass(myvar)’ which in turn would run ‘__init__’. 11 Instantiate: An object is called an instance of its class. The process of creating an object from a class is called instantiation. Argument: An argument is the actual value of a parameter (e.g. “David Moodie”). A parameter is a variable (e.g. mynamestring) passed to another part of code (such as a function).
Python OOP – David Moodie The ‘__init__’ method then gets the passed variable as it’s 2 nd parameter. In this case the value of ‘myvar’ used in instantiation gets picked up by the local variable ‘string’ as seen in this example. The first argument, ‘self’, in the ‘__init__’ definition is necessary for all methods in a python class. Within a class method, access to instance properties is done by using ‘self.property’. ‘self.name = string’ shown above, sets the ‘name’ attribute of the instance to the value of the ‘string’ parameter. This means that to use a current instance’s attributes (variables / methods) in any method within the class, you must precede the attribute with ‘self.’. 12
Python OOP – David Moodie The actual algorithm used here for returning the initials of a name is not important. What is important is that you notice the self argument being passed to the initials method. So even though we don’t want to pass a value to the ‘initials’ method we still need to use the self argument to give the method access to the instance’s attributes. As you can see we are accessing the name attribute by using the reference ‘self.name’ which was set in the ‘__init__’ method. The reason why we can’t just use a global ‘name’ variable in the class is that we want each instance of the class to have different name attributes. Each person needs a different name. 13
Python OOP – David Moodie The first statement sets ‘myname’ to an instance of the ‘name’ class with an initialisation variable “David Moodie”. The second statement shows the use of the method ‘initials()’ on the instance ‘myname’. As you can see, no variable was passed directly to the initials method. Instead the method can use the self.name attribute set in the ‘__init__’ method. 14
Python OOP – David Moodie In Summary A class is a ‘blueprint’ from which objects can be created. Each object can have unique property values and may have ‘overridden’ class methods or just inherited them from the parent class. A class is preferable when multiple data structures of the same type need to be created. Such as a ‘creature’ class having child classes of specific animals or enemies in a game. The main benefits of OOP are code structure, organisation, code reuse and application extensibility. 15