Learn Python CLASS METHODS in 6 minutes! 🏫

preview_player
Показать описание
# Class methods = Allow operations related to the class itself
# Take (cls) as the first parameter, which represents the class itself.

# Instance methods = Best for operations on instances of the class (objects)
# Static methods = Best for utility functions that do not need access to class data
# Class methods = Best for class-level data or require access to the class itself

class Student:

count = 0
total_gpa = 0

def __init__(self, name, gpa):

#INSTANCE METHOD
def get_info(self):

@classmethod
def get_count(cls):

@classmethod
def get_average_gpa(cls):
return 0
else:

student1 = Student("Spongebob", 3.2)
student2 = Student("Patrick", 2.0)
student3 = Student("Sandy", 4.0)

Рекомендации по теме
Комментарии
Автор

# Class methods = Allow operations related to the class
# Take (cls) as the first parameter, which represents the class itself.

# Instance methods = Best for operations on instances of the class (objects)
# Static methods = Best for utility functions that do not need access to class data
# Class methods = Best for class-level data or require access to the class itself

class Student:

count = 0
total_gpa = 0

def __init__(self, name, gpa):
self.name = name
self.gpa = gpa
Student.count += 1
Student.total_gpa += gpa

#INSTANCE METHOD
def get_info(self):
return f"{self.name} {self.gpa}"

@classmethod
def get_count(cls):
return f"Total # of students: {cls.count}"

@classmethod
def get_average_gpa(cls):
if cls.count == 0:
return 0
else:
return f"Average gpa: {cls.total_gpa / cls.count:.2f}"

student1 = Student("Spongebob", 3.2)
student2 = Student("Patrick", 2.0)
student3 = Student("Sandy", 4.0)

print(Student.get_count())

BroCodez
Автор

nice video dude your tutorial are always a class

Rizzlers_Edits
Автор

I think the reason class method gets so confusing to understand is because in most of the examples taken, people operate on a class variable. First it is defined, then some operation is performed on it in __init__ or some other instance method, then finally calls @classmethod decorator and performs some operation on that class variable. Reason it is confusing bcoz this can be done using self too. Another example is, calling a class method which creates you an object based on some input...again this can also be done w/o class method. There needs to be some tutorial on internet which explicitly explains the use cases of class methods which can't be done by others

shantanughosh
Автор

It is a very confusing topic but one of the best explanation thanks brocode ❤

uzairzarry
Автор

I liked your react course!. Can you make Angular?

qryzvet
Автор

Thanks bro ... Can you please mak a video on python generators please please please

robert-qnhy
Автор

Do a video about java ProcessBuilder please!

jadotati
Автор

never knew the keyword cls even exist lol
thanks for the content!

xfbusgg
Автор

Dear Bro Code,

Pls can you do a full 12hours course on Django. Also using rest framework for creating endpoints and building backend applications


Am a beginner. I really love your courses and I must admit I like the way you teach bro code


I would be very delighted if you do a full 12hours course on Django and also teach us everything on the backend

I would also appreciate it if you used the same approach you adopted while doing the JavaScript 12hours full course where it was practical and project based.


I am my friends love your series

Pls bro code consider this as a special request from a fellow bro💓🥺

Thanks bro code💓

menegideon
Автор

College fees : -13000$
Bro code : 0 $ + guidence

talktoA.I
Автор

i love brocode, wanna be my step bro?

aryyann
Автор

I watch your videos love from India can i get your ig? ❤❤

mrbaba
Автор

I wrote this program: task = []

def add():
add_input = input("What do you want to add to your tasks list?: ")
task.append(add_input)
print(task)
def less():
less_input = int(input("Enter the index you want to delete "))
task.pop(lessinput - 1)
print(task)
def view():
print(task)
while True:
choices = input("a.Enter the task you want to add to your list\nb.Enter the task you want to deduct to your list\nc.View you tasks\nd.Stop\nEnter the choice you would like: ")
if choices == "a":
add()
elif choices == "b":
less()
elif choices == "c":
view()
elif choices == "d":

break
else:
print("INVALID INPUT")

jayanthkadlur