Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Python Instantiation & Understanding Self

Similar presentations


Presentation on theme: "OOP Python Instantiation & Understanding Self"— Presentation transcript:

1 OOP Python Instantiation & Understanding Self
The OOP Paradigm

2 Activity 1 Load up your code from last lesson ‘The Animal Class’
Remind yourself of: What a class is; What attributes are; What methods are; How objects are made; How we can access an objects attributes; How we can call an objects methods.

3 Learning Habits Reasoning: Noticing details Adapting :
Thinking rigorously, methodically and giving explanations. Noticing details Adapting : Reflecting and making changes Empathising… …with feelings and views Effective use of time Questioning: Asking questions to get below the surface Listening… …to understand Imagining… …how things could be and seeing a range of possibilities Distilling… …what you have learnt and what you need to learn Collaboration: Working effectively with others Meta Learning: Talking about how you have been learning Imitation: Picking up good habits from others Independence: Working effectively alone Managing distractions… …and sustaining concentration Capitalising: Using resources purposefully Making links… …and recognising relevance Perseverance: Overcoming frustration and difficulty Planning… …your learning in advance

4 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Introduction Last lesson we looked at creating a class Remember that a Class is a template / blueprint from which we can create objects. And remember that an Object is simply a bundle of related code. In the example above, we have created a class (Human) which has 4 attributes which are set up with data from the constructor. The example class also has a method called ‘speak’.

5 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Instantiation (creating objects) The creation of objects is called ‘instantiation’. When creating an object we create an instance of the class. To create a human object from our human class we write the following code in the main section of our program: Here, we have created a human object which now has the attributes ‘name’, ‘age’, ‘nationality’ and ‘gender’ and can also ‘speak’.

6 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Instantiation (creating objects) Now that the human object ‘bob’ is created, we can get data from ‘bob’ and get ‘bob to speak’. Here, we are accessing the objects attribute values and methods.

7 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Accessing objects attribute data: To access an object’s data, we simply write the name of the object followed by… .attribute_name

8 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Accessing objects methods: To access an objects method, we simply write the name of the object followed by… .method_name()

9 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Instantiation with unique/new data (passing parameters into the constructor) Because we set up the constructor method of our class to pass values into the newly created attributes… …all objects created… …will all have the same attribute values. That is, unless, we pass new values into the constructor at the moment of instantiation.

10 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Instantiation with unique/new data (passing parameters into the constructor) To do this we can add data into the brackets (next to the class name) which will pass into the constructor, overwriting the default attribute values (e.g. ‘no name’, 0, ‘no nationality’ etc.).

11 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams Instantiation with unique/new data (passing parameters into the constructor) The arrow is representing the passing of the parameters (“Bob”, 42, “British”, “Male”) as the object ‘bob’ is being instantiated. They enter the constructor method’s brackets in the same order as they are written in the instantiation statement and overwrite any default values (e.g. “no name”, 0, “no nationality”, “gender”).

12 Activity 2 Open up your code from last lesson (which should now have a completed class for an ‘animal’). Instantiate a new object called Dog. But instead of the dog having the default values from the class, pass new values into the class at instantiation. animal_species = “Dog” Age = 10 threat_level = 8 hunger_level = “Snarly” Then print each of the new object’s attributes

13 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Understand Encapsulation and Private Attributes Inheritance UML and Hierarchy Diagrams What is self?! Self is a reference to the bound object: When each object is created, the object itself is passed into the class’s parameter called ‘self’. Now, there is a reference to the bound object. So, whenever the object updates it’s attribute values, python can keep track of them.

14 Object Orientated Programming
What is self?! 1. When objects are instantiated, the object itself is passed into the self parameter. 2. Because of this, the object’s data is bound to the object. Below is an example of how each object’s data might look. Notice how ‘self’ is replaced with the objects name.

15 This is due to the ‘self.’ prefix.
The code below shows the instantiation of 2 objects and the result of accessing their individual data. Kate’s data being passed into the constructor method at the moment of instantiation. Bob’s data being passed into the constructor method at the moment of instantiation. In the code, you can see when we access the objects’ data, each object has its own individual data. It manages to keep track of it. This is due to the ‘self.’ prefix.


Download ppt "OOP Python Instantiation & Understanding Self"

Similar presentations


Ads by Google