Class Methods in Python | How to add Methods in Class | Python Tutorials for Beginners #lec87

Jenny's Lectures CS IT
22 Jul 202320:49

Summary

TLDRThis video tutorial explains how to add methods to a class in Python, building upon previous lessons on attributes and object creation. It highlights the difference between attributes (the characteristics of an object) and methods (the actions an object can perform). The video demonstrates how to define and use the `__init__` method to initialize attributes and explains the importance of the `self` keyword in binding attributes and methods to objects. It also covers default values, class object variables, and provides a practical example of adding and updating followers for an instructor class.

Takeaways

  • 🛠️ In this video, methods are added to a class in Python, continuing from the previous video on attributes.
  • 📜 Attributes represent the data objects possess, such as name, phone number, and address, while methods represent actions that objects can perform.
  • 💡 The `__init__` function is introduced, which is used to initialize attributes when creating an object of a class.
  • 🏗️ Methods in a class are similar to functions but are attached to objects, making them distinct from standalone functions.
  • 🔗 The `self` keyword is crucial in class methods, binding the method to the specific object it is called on, allowing it to access that object's attributes.
  • 📝 Class object variables, like `followers = 0`, are shared by all objects of the class, making them accessible without explicitly initializing them for each object.
  • 🔄 The method `update_followers` is created to modify object attributes (like increasing a follower count), showing how methods interact with attributes.
  • 📥 Parameters passed to class methods don't need to be attributes of the class (e.g., `subject_name`), but attributes (like `self.name`) must use the `self` keyword to access.
  • ⚙️ Methods allow objects to perform actions, such as updating attributes based on interactions (like increasing a follower count when someone follows).
  • 🤔 Understanding the difference between passing arguments to methods and using attributes is key when designing classes with both attributes and methods.

Q & A

  • What are the two main components of a class in Python?

    -The two main components of a class in Python are attributes and methods. Attributes represent the properties of the object (e.g., name, address), while methods represent the actions the object can perform (e.g., teaching).

  • What is the purpose of the __init__ method in a Python class?

    -The __init__ method is a constructor used to initialize the attributes of a class when an object is created. It assigns values to the attributes and ensures the object is properly set up with the required properties.

  • Why is the 'self' keyword important in Python class methods?

    -The 'self' keyword refers to the instance of the class on which the method is being called. It allows access to the attributes and methods of the specific object, ensuring that operations are applied to the correct instance.

  • What is the difference between a class attribute and an instance attribute?

    -A class attribute is shared by all instances of the class, while an instance attribute is specific to each object. In the example, 'followers' is a class attribute (shared by all objects), whereas 'name' and 'address' are instance attributes (specific to each object).

  • How can you call a method of a class in Python?

    -To call a method of a class, you use the object name followed by the method name. For example, 'instructor1.display()' calls the 'display' method on the 'instructor1' object.

  • What happens if you don't provide a return statement in a method?

    -If a return statement is not provided, the method will automatically return 'None'. This is why in the script, the output included 'None' until the return statement was adjusted.

  • Why did the script produce an error when 'self.subject_name' was used instead of 'subject_name'?

    -The error occurred because 'subject_name' was passed as a parameter, not an attribute of the class. 'self' is only used to access attributes of the object, and 'subject_name' was just a local variable in the method.

  • What is the purpose of the 'update_followers' method in the script?

    -The 'update_followers' method increases the follower count of an instructor by one each time it's called, simulating someone starting to follow that instructor.

  • What is the significance of default values in class attributes?

    -Default values in class attributes, such as 'followers = 0', ensure that every object starts with the same value for that attribute unless explicitly changed. This avoids needing to pass the same value every time an object is created.

  • How can the script be extended to handle both 'followers' and 'following' counts for an instructor?

    -The script can be extended by adding a new attribute, 'following', to track how many people the instructor is following. A corresponding method could be added to update the 'following' count whenever the instructor starts following someone.

Outlines

00:00

👨‍🏫 Introduction to Class Attributes and Methods

This paragraph reviews the previous video's topics on creating a class, adding attributes, and defining objects. It introduces the concept of attributes as properties of an object, such as name and address, and methods as actions the object performs. The video will now focus on adding methods to a class, building on prior knowledge of the `__init__` function and class structures. The use of Pascal case for class naming conventions is also briefly discussed.

05:01

📝 Adding Methods to a Class

The explanation begins with creating a new file for class methods, using the example of an instructor class with name and address attributes. It demonstrates how to pass values to these attributes using the `__init__` function and explains the role of `self` in binding attributes to arguments. The instructor objects are created with name and address attributes, and their values are printed to show how attributes can be accessed. This paragraph reinforces the concept of the `self` keyword and how it dynamically refers to the object being created.

10:03

📊 Introducing Default and Class Object Variables

Here, the concept of default attributes and class object variables is introduced. The `followers` attribute is set with a default value of 0, either passed in the `__init__` method or as a class object variable. The paragraph explains that class object variables are shared across all instances of the class, whereas instance variables are unique to each object. Examples are provided to show how these variables work, emphasizing the distinction between class object variables and instance attributes.

15:04

🔧 Defining and Calling Methods on Class Instances

This section covers the process of adding methods to a class. The structure of a method within a class is similar to defining a function, with `self` being automatically passed as the first argument. The example of a `display()` method is used to demonstrate how methods are called on objects and how `self` binds the method to the specific object instance. An error when calling a method without `self` is explained, and the use of the method with the correct syntax is demonstrated.

20:05

💡 Handling Return Values and Method Refinement

This paragraph explains the behavior of functions that don’t have an explicit return statement, highlighting that they return `None` by default. It demonstrates how this can lead to unexpected output and shows how to refine the method to avoid printing `None`. The example is extended to include more functionality in the method, such as printing a personalized message using an object’s attributes (e.g., name). The role of `self` in accessing these attributes within the method is further clarified.

🚀 Passing Arguments to Methods

The paragraph introduces the idea of passing arguments to methods, using an example where a subject name is passed to the `display()` method. The difference between using an instance attribute (`self.name`) and a method argument (subject) is discussed in detail. It clarifies why `self` is used for attributes but not for passing simple arguments, using errors as examples to illustrate the correct usage.

📈 Updating Object Attributes with Methods

A new method, `update_followers()`, is introduced to demonstrate how methods can modify the attributes of an object. The method increments the `followers` count of an instructor by 1 when a new follower is added. The example shows how calling the method on different objects affects their individual `followers` attributes. The paragraph emphasizes the use of `self` in updating object-specific attributes and explains how to pass additional arguments to methods when necessary.

🔄 Managing Multiple Objects and Their States

This section expands on the `update_followers()` method by applying it to multiple instructor objects. The followers count is updated for each instructor, and the differences between their follower counts are printed to show how each object maintains its own state. The paragraph demonstrates the use of methods to interact with and modify object attributes dynamically, encouraging the reader to experiment with adding more objects and updating their attributes.

🎯 Applying the Concepts: Followers and Following Counts

The final paragraph encourages viewers to experiment further by creating additional instructor objects and managing both follower and following counts, drawing parallels to social media platforms like Instagram. The suggestion is to explore how followers and following counts can be tracked for each instructor, using methods to manage these attributes. The paragraph concludes by introducing the upcoming video, which will feature an exercise based on the concepts discussed.

Mindmap

Keywords

💡Class

A class is a blueprint for creating objects in object-oriented programming. It defines a set of attributes and methods that the created objects can have. In the video, the 'Instructor' class is used as an example to show how attributes like 'name' and 'address' and methods like 'display' are added to a class.

💡Object

An object is an instance of a class, representing a specific entity that has the attributes and behaviors defined by the class. In the video, examples of objects include 'instructor1' and 'instructor2', which are instances of the 'Instructor' class, each having its own 'name' and 'address'.

💡Attribute

Attributes are characteristics or properties of an object. In the video, attributes like 'name', 'address', and 'followers' are assigned to each object of the 'Instructor' class, representing the data associated with each specific instructor.

💡Method

A method is a function defined within a class that describes the behaviors or actions an object can perform. In the video, methods like 'display' and 'update_followers' are introduced to show how objects can interact with their attributes, such as printing a message or updating follower count.

💡self

The 'self' keyword is a reference to the current instance of a class. It is used to access the attributes and methods of the object within the class methods. In the video, 'self' binds attributes like 'name' and 'address' to the specific instance of the 'Instructor' class, ensuring that each object maintains its unique properties.

💡__init__ method

The '__init__' method is a special function in Python used to initialize an object's attributes when it is created. In the video, the '__init__' method is used to set the 'name' and 'address' of an 'Instructor' object when a new instance of the class is instantiated, allowing for personalized data to be stored for each object.

💡Pascal Case

Pascal Case is a naming convention in which the first letter of each word is capitalized. In the video, it is mentioned as a convention for naming classes, like 'Instructor'. This helps differentiate class names from variable and function names, although not using it won't cause a syntax error.

💡Class Object Variable

A class object variable is shared across all instances of a class. In the video, the 'followers' attribute is an example of a class object variable, as its value is initialized to '0' for all instructor objects. Each instructor object shares this attribute, and its value can be updated across instances.

💡F-string

F-strings in Python are a way to embed expressions inside string literals using curly braces. In the video, an F-string is used to print dynamic information like 'name' from the 'Instructor' class in a method that displays a message, such as 'Hi, I am Jenny and I teach Python'.

💡Positional Arguments

Positional arguments are arguments that need to be passed in the correct order to a function or method. In the video, when creating an object of the 'Instructor' class, positional arguments like 'name' and 'address' need to be passed in the right sequence, or else the program would raise an error.

Highlights

Overview of adding attributes and methods to a class in Python.

Attributes represent the characteristics an object has, like name, phone number, etc.

Methods represent the actions an object performs, like teaching, displaying information, etc.

Introduction to the `__init__` method to initialize class attributes.

Explanation of `self` keyword: it binds attributes to the arguments passed during object creation.

Naming conventions for classes, like using Pascal case (capitalized first letter).

Demonstration of creating and passing attributes like `name` and `address` when initializing objects.

How the `__init__` method is called each time an object is created.

Use of default attribute values like `followers = 0` for every object in the class.

Difference between instance attributes (those inside `__init__`) and class-level attributes.

Introduction to methods as functions attached to an object and how to define them using `def` keyword.

Importance of using `self` in methods to bind them to the specific object they're called on.

Explanation of why methods return `None` if no explicit return statement is given.

Using f-strings for dynamic printing inside methods, such as `Hi, I am {name}`.

Creating methods to modify class attributes, such as updating the follower count for an instructor.

Adding and passing arguments to methods for customized behavior, like specifying the subject an instructor teaches.

Transcripts

play00:04

in the previous video we have seen how

play00:07

to add attribute to your class right how

play00:10

to create your own class how to create

play00:12

object and then how to add attribute

play00:13

right now remaining is obviously in

play00:16

class we are having like attribute as

play00:18

well as methods attribute means the

play00:21

object the you know attributes are

play00:23

basically the things that the object has

play00:26

like I am if I am an object then I'm

play00:28

having my name my phone number address

play00:30

these are objects and methods are the

play00:33

thing that the object does like I do

play00:37

like I do teach right so these are the

play00:40

methods so now in this video we'll see

play00:42

how to add methods to your class right

play00:46

so we have seen in the previous video

play00:47

init function as well how to initialize

play00:50

the attributes right so you please you

play00:54

first watch that video and then come to

play00:56

this video right then you will get it

play00:58

better because for this to understand

play01:01

this video you need to know what is that

play01:02

underscore underscore in it underscore

play01:04

underscore in bracket you have just

play01:06

passed cell what is that function right

play01:10

okay now

play01:11

this we have done in the previous video

play01:13

see the class was instructor

play01:16

okay the class name it should be capital

play01:19

i

play01:20

uh Pascal case uh the convention is

play01:23

Pascal case v used to name the class but

play01:27

if you you know take small I then it's

play01:30

also fine it will not give any syntax

play01:31

error but

play01:33

it is like the convention is basically

play01:35

to differentiate the class name variable

play01:37

name function name and like this right

play01:39

okay now

play01:41

we will add methods to our class

play01:45

so we are going to create a new file

play01:48

here

play01:53

class methods Dot py

play01:56

and let's take we are having same

play01:58

example I'll be taking we are having a

play02:01

class instructor right and now that init

play02:07

function

play02:08

and we are passing like two two

play02:11

parameter here one is the name and

play02:13

second one we have passed that was

play02:16

address so how to assign the value then

play02:19

self Dot

play02:21

name of the attribute is I'm taking name

play02:24

and here name

play02:27

so see these this and this should be

play02:30

same the parameter name and this value

play02:32

this should be

play02:34

same this name can be different

play02:38

it is not compulsory that this this name

play02:40

of this attribute should be same as the

play02:42

parameter name right these things we

play02:45

have discussed

play02:46

you know in previous video

play02:49

so let's take address and this is what

play02:53

address I am assigning

play02:55

and if you create

play03:02

an object of the class instructor then

play03:06

this time we need to pass

play03:09

if I run this then C you will get an

play03:11

error

play03:12

this instructor is missing two required

play03:14

positional argument line number five

play03:15

here we are creating an object so you

play03:19

have to pass name as well as address so

play03:22

one passing like name is equal to Jenny

play03:24

and suppose address is

play03:27

gurgaon

play03:28

so this time it will not give any error

play03:30

and if you print suppose instructor one

play03:33

wrote name then it will give you Jenny

play03:37

this is how we can access right so you

play03:40

know the self is just a keyword that

play03:43

bind the the the attribute

play03:47

attributes to the argument receive so

play03:50

this init function would be called every

play03:52

time you create an object so if you

play03:55

create

play03:56

two object then two time this init

play03:58

function would be called right so first

play04:00

time it would be called while we are

play04:02

creating this object at in Fifth Line

play04:05

while we are executing the Fifth Line so

play04:08

self means self is referring to the

play04:10

actual object now so self is referring

play04:12

instructor one so self is instructor one

play04:15

at this point of time so instructor one

play04:16

dot name whatever name you will pass

play04:18

Jenny instructor one dot address

play04:21

whatever address you will pass that is

play04:23

right now if I create another object

play04:26

same

play04:28

and instructor 2 is suppose

play04:31

Gia

play04:35

so at this point of time also this init

play04:38

function would be called every time you

play04:40

create an object this function would be

play04:42

called every time so now if we are

play04:44

calling

play04:45

if this function is being called during

play04:47

this time then this time the self is

play04:49

referring to instructor to this object

play04:51

so now self value is you can assume like

play04:54

instructor 2 and name value is Gia

play04:57

addresses same gurgaon so now selfish

play05:00

instructor 2 dot name gr instructor to

play05:02

dot address

play05:04

I hope now you got the basically the

play05:06

other working why we are using this self

play05:08

here right it is binding

play05:11

the now object attribute to the argument

play05:14

received right okay

play05:17

now we we are we we have a default also

play05:21

like default

play05:22

attribute that is followers because it

play05:25

is not compulsory that every teacher is

play05:27

on phone every teacher is on sorry self

play05:29

dot Instagram

play05:32

so by default rather than passing zero

play05:34

zero zero for every teacher if you have

play05:36

created 10 object then every time you

play05:39

will pass zero zero zero as an argument

play05:40

if you write down here followers

play05:43

as a parameter but set the default value

play05:46

so by default you don't need to pass 0

play05:48

here now followers would be 0 0 for

play05:50

every object you create right or rather

play05:52

than this one more ways what you can

play05:53

create a class object variable here

play05:57

followers is equal to 0 rather than here

play06:02

so now this here see we are not using

play06:04

any cell keywords self growth follower

play06:07

no because these are outside of this

play06:09

init function so this is what a class

play06:12

object variable for every object that

play06:15

you create from this class instructor

play06:17

instructor class the follower this

play06:20

attribute would be

play06:21

Associated to every object and for every

play06:24

object the value of polar would be zero

play06:25

so see if you want to check then if you

play06:28

want to print like instructor

play06:30

one dot

play06:32

followers then you will give you will

play06:35

get 0 see

play06:38

although we haven't initialized this we

play06:40

are not passing any follower here we

play06:41

haven't initialized here self dot

play06:43

follower zero so where from where this

play06:46

is taking the value follow a zero for

play06:48

instructor 2 also follow us value is 0.

play06:50

right now this is what just a quick

play06:53

recap of the previous one because we are

play06:55

just adding the attribute we have just

play06:57

started attribute now how to add methods

play06:59

so methods are basically

play07:01

simple you can say these are functions

play07:04

like if

play07:06

same

play07:07

what is the syntax of creating a

play07:10

function def function name and round

play07:13

bracket and if you want to pass any

play07:14

parameter just pass the parameter right

play07:16

same

play07:18

you can say that um

play07:20

syntax is here depth function name

play07:23

suppose simple I am taking display

play07:25

function

play07:27

and if you something like this if you

play07:30

put these bracket then automatically

play07:32

here you will get self keyword

play07:35

because this is compulsory

play07:37

on every if you call this display method

play07:42

on this instructor one then the self

play07:46

will bind this function to

play07:50

this object on which you are calling

play07:52

this display so at this point of time

play07:54

self is a structure one if you are

play07:56

calling this display function on

play07:58

instructor 2 object second object then

play08:00

at that point of time self will become

play08:02

instructor 2. so it will basically bind

play08:04

right

play08:06

the function or the method

play08:09

to the object on which you are calling

play08:11

this method so means a function becomes

play08:15

method when it is attached to a

play08:17

particular object right that is why we

play08:20

are not calling it function we are

play08:21

calling it method so here I am simply

play08:24

printing

play08:25

like print

play08:27

and

play08:28

suppose I print hi

play08:31

that's it this is the simplest example

play08:34

I'm taking right and on instructor 1 how

play08:37

to call this method now if you call this

play08:40

if you access this method using first

play08:42

object

play08:43

then simply we cannot write down here

play08:46

like display

play08:48

and just call display function no it

play08:51

will give error

play08:52

see the name display is not defined in

play08:56

line number 11 right

play08:58

so you have to attach this

play09:01

with the object name

play09:04

so on instructor 1 I want to call this

play09:06

display function so instructor 1 dot

play09:08

display right this is how an object can

play09:11

access

play09:12

the method of a class or attribute of a

play09:14

class object name dot then whatever you

play09:17

want to access display okay if you run

play09:19

this

play09:21

see you are getting

play09:23

Jenny high and C yeah you are getting

play09:26

none here

play09:28

so

play09:30

where is the problem why you are getting

play09:31

this none

play09:33

so please pause the video and try this

play09:34

out why you are getting Jenny High then

play09:36

none sorry

play09:39

so see the problem is

play09:42

it will print Jenny now we are calling

play09:45

in print we are calling instructor one

play09:46

dot display now we are calling this so

play09:49

control would be here right now self is

play09:52

this time instructor one so it will

play09:54

print in display we are having just

play09:56

print hi so it will print hi now you are

play09:59

printing means whatever this this will

play10:03

return that thing we want to print but

play10:06

here we are not

play10:07

writing any return statement

play10:09

so if you don't write any return

play10:11

statement then definitely we know every

play10:13

function is going to return something if

play10:14

you do not write any return statement

play10:16

then it will return none so that is why

play10:18

it is returning none here and we are

play10:21

printing none we are getting the answer

play10:24

Jenny higher than none so just remove

play10:27

this print

play10:28

and simply call display function on

play10:31

instructor one object right now let's

play10:33

run this

play10:34

see Jenny and hi

play10:37

right I hope you got this

play10:39

now this is the simplest example you

play10:40

have taken now suppose basically methods

play10:43

are basically you know the actions we

play10:45

perform on the attributes may be using

play10:48

the values of the attributes and do

play10:50

something or maybe modify the attribute

play10:52

value and do something and return some

play10:54

result

play10:55

that is obviously the use of methods it

play10:58

is not about you know any use like just

play11:01

print hi but this is just to make you

play11:03

understand like how you can define a

play11:06

function right now if you want to print

play11:08

if suppose I want to call this one here

play11:11

so it should print hi I am I am genuine

play11:15

because instructor one name is Jenny

play11:19

so can you do this

play11:20

yes you can pause the video and try this

play11:23

out

play11:24

so hi I want to print hi

play11:28

I am gen so from where I will get Jenny

play11:35

from the name attribute

play11:37

in name attribute of instructor 1 I have

play11:40

gen so from the name attribute just

play11:43

fetch the value so directly if you write

play11:46

name here so obviously you have to use F

play11:48

string

play11:51

then what do you think

play11:52

you will get Jenny or not let's run this

play11:56

C

play11:57

it is giving an error

play11:59

here display line number two and line

play12:01

number eight High I am this name name is

play12:04

not Define

play12:05

name is not defined just it's just a

play12:08

variable and we are using here not just

play12:09

simple variables attributes you have to

play12:11

attach this variable to a specific

play12:14

object

play12:16

for Which object you are using this name

play12:18

obviously instructor one so rather than

play12:21

obviously instructor one dot name here

play12:23

you will write

play12:25

self one note name so sorry self dot

play12:28

name because here self is referring to

play12:32

that particular object on which you are

play12:33

calling this display function so here

play12:35

cell phone is instructor one so

play12:37

automatically it will fetch the name

play12:39

from instructor one hi I am Jenny

play12:42

right I hope you got this so the self

play12:45

keyword you will get you know many times

play12:48

within this class only not outside of

play12:50

the class we are using cell we are using

play12:52

within this class only cell right and

play12:56

my name is Jenny and suppose I want like

play12:59

I teach python

play13:02

so

play13:03

I want to pass a parameter an argument

play13:06

as an argument what subject I teach so

play13:08

for that suppose in display I want to

play13:11

pass like I teach

play13:14

python

play13:16

so for this obviously you need some

play13:18

parameter to fetch to you know uh store

play13:21

that value that string so

play13:24

I'm just taking a parameter name subject

play13:26

name right hi I am the name and

play13:31

I

play13:33

teach

play13:34

now how you will fetch that thing I

play13:36

teach the subject name the python python

play13:40

it should print hi I am Jenny and I

play13:42

teach python

play13:43

so you can pause the video and try this

play13:45

out

play13:46

okay I hope you have done this and you

play13:49

just need to write down here

play13:52

subject name no need to write down self

play13:55

load subject name

play13:57

first let's run this and see

play13:59

I'm Jenny and I and I teach python why

play14:02

you are not writing here self

play14:04

dot pipe so uh dot subject name if you

play14:07

write this thing let's run this and you

play14:09

will see you will get a error message

play14:13

like in line number 12 here you are

play14:17

calling this and then in line number

play14:18

eight line number eight means

play14:21

uh

play14:23

here here you are defining this function

play14:26

right now what this is this line

play14:31

an attribute error it's an attribute

play14:33

error instructor object has no attribute

play14:35

subject name because when you access

play14:39

self Dot like this then this should be

play14:42

an attribute

play14:44

because I am using self dot name because

play14:46

name is an attribute of

play14:48

that object

play14:50

it's an attribute in this class but

play14:52

subject name is not any attribute we

play14:54

have only two attribute name and address

play14:56

right so how this self can bind

play15:00

because this can only bind the attribute

play15:03

this attribute to that particular object

play15:06

but subject name is not any attribute we

play15:08

are just passing

play15:09

an argument so simply you will pass

play15:12

Python and it will take the value from

play15:14

subject name python no need to write

play15:16

down here self load

play15:18

subject name I hope now you got the

play15:20

difference why I am writing a self dot

play15:22

names and here just subject name we are

play15:25

not passing any name here and we are

play15:26

still getting name Jenny

play15:29

right because it is an attribute of the

play15:31

object and we are passing subject name

play15:33

and we are getting obviously the subject

play15:34

name right

play15:36

so now

play15:39

if you don't know like maybe subject

play15:42

name if I pass like python simply python

play15:45

then it will give error

play15:46

python is not defined in line number 12.

play15:49

right

play15:51

because this this is expecting a string

play15:56

as an argument

play15:57

so like the proper ways you can just

play16:00

write down here in comments like hash

play16:03

and

play16:04

this will accept subject name as a

play16:07

string

play16:08

right or expect a string

play16:11

as a subject name so to Define that

play16:14

thing you can write down here that

play16:15

proper thing so that if anyone see your

play16:19

program then he or she will get yeah

play16:21

they have to pass

play16:23

string right so this should be string

play16:27

here

play16:28

and if suppose if you want to update the

play16:31

follower count right so if suppose I

play16:34

take another

play16:35

function depth

play16:41

update followers right and obviously it

play16:44

will be self

play16:46

and

play16:48

on instructor one I am calling this

play16:51

function on instructor 1 Dot

play16:54

update follower right and here

play16:58

I want I just pass a name it means that

play17:02

person has started following instructor

play17:04

one so the follower count of instructor

play17:06

1 would be increased by one right so the

play17:11

follower count of

play17:14

self load

play17:15

followers of that object on which we are

play17:18

calling this function so definitely you

play17:19

will write yourself self dot followers

play17:21

count will increased by one right so

play17:26

here I am passing a suppose name

play17:29

like pile means pile has started

play17:33

following

play17:34

instructor one and instructor one is

play17:36

Jenny means pile has started following

play17:38

Jenny so Jenny's followers would be plus

play17:41

one right and now if you print

play17:47

like instructor one dot

play17:50

followers

play17:52

then see what you will get

play17:55

okay it is giving an error because in

play17:57

line number 17 here we are passing

play18:00

instructor type error it takes one

play18:03

positional argument but two are given

play18:05

okay

play18:06

to accept obviously to accept this pile

play18:09

you have to

play18:11

give up obviously the parameter name

play18:14

because one is self to and one is pile

play18:18

for pile to accept pile you have to give

play18:21

a parameter name follow our name

play18:23

see now hi I am Jenny I teach Python and

play18:25

one now follower count of Jenny is one

play18:27

it's starting it was Zero now see you

play18:30

have updated the attribute

play18:33

right

play18:34

so this also you can do if if suppose I

play18:37

have one more instructor instructor too

play18:39

and on instructor two for instructor two

play18:42

I am just printing

play18:45

followers

play18:48

then it will give zero

play18:50

see one was for instructor one and at

play18:54

last we are getting zero so that is for

play18:56

instructor two because on instructor 2

play18:57

you haven't pulled this update follower

play18:59

function right if you call this update

play19:02

folder function on instructor 2 as well

play19:04

like instructor two Dot

play19:06

update follower and

play19:09

suppose Jenny has started following

play19:13

instructor to instructor 2 is Gia

play19:16

so let's run this

play19:18

now both are having

play19:23

two two obviously you have to

play19:26

okay

play19:27

still zero because

play19:29

we are printing

play19:31

here so after update after calling this

play19:34

update you have to you have to print

play19:36

instructor to wrote follower right see

play19:39

now both

play19:40

one and one

play19:42

for both it's one and one right four

play19:45

over count

play19:47

so this is how you can add methods and

play19:50

so you can I hope you got how to add

play19:53

methods and when you use self load the

play19:56

name and like without self uh where when

play19:59

you can write down any variable name or

play20:01

attribute name sorry this is not

play20:03

attribute this is just a variable right

play20:05

or argument you are passing

play20:07

fine

play20:08

so now you can try this out like maybe

play20:11

you can update

play20:12

for maybe you can create two more

play20:15

objects and you can update the followers

play20:17

right or maybe you can try to print like

play20:20

follow account as well as following

play20:22

count if I started like someone GS

play20:25

started following me so my follow

play20:27

account would be one and Gia's following

play20:30

count would be one right followers and

play20:32

following if you are on Instagram then

play20:34

you know this thing right followers and

play20:36

following two things are there

play20:39

so you can play around with this code

play20:42

So based on this we'll be discussing one

play20:44

exercise in the next video so I'll see

play20:46

the next video bye take care

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Python classesOOP methodsClass attributesCoding tutorialProgramming tipsMethods in PythonPython objectsSelf keywordPython basicsSoftware development
Benötigen Sie eine Zusammenfassung auf Englisch?