Learn Python CLASS METHODS in 6 minutes! 🏫

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.พ. 2025
  • 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):
    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())
    print(Student.get_average_gpa())

ความคิดเห็น • 31

  • @BroCodez
    @BroCodez  7 หลายเดือนก่อน +5

    # 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())
    print(Student.get_average_gpa())

    • @gamingfromhell1072
      @gamingfromhell1072 7 หลายเดือนก่อน

      Can u Please make a django tutorial

  • @Rizzlers_Edits
    @Rizzlers_Edits 7 หลายเดือนก่อน +5

    nice video dude your tutorial are always a class

  • @Hassnaeerr
    @Hassnaeerr หลายเดือนก่อน +1

    Meilleurs explication ❤❤

  • @TheQbbxrxr
    @TheQbbxrxr 10 วันที่ผ่านมา

    you are so good at explaining! thanks

  • @shantanughosh655
    @shantanughosh655 5 หลายเดือนก่อน +3

    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

  • @Irina.Parvanova
    @Irina.Parvanova 16 วันที่ผ่านมา

    Thank you so much for explaining it so well! So many people tried to explain it to me without success. I even cried my eyes out...
    Every time I watch your videos I understand it perfectly! Thank you, thank you, thank you!

  • @uzairzarry8691
    @uzairzarry8691 7 หลายเดือนก่อน +2

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

  • @Om-jo8eu
    @Om-jo8eu 7 หลายเดือนก่อน +1

    Cool video bro

    • @youtubeok4475
      @youtubeok4475 7 หลายเดือนก่อน

      Hey brother 😂 first watch the vdo then say some praise

  • @sniegbanan
    @sniegbanan 17 วันที่ผ่านมา

    Hi, couldnt you use count = False instead of 0 and in avg func go like if cout: return avg return 0 ?

  • @NasrKhalil-y3f
    @NasrKhalil-y3f 7 หลายเดือนก่อน

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

  • @jadotati7641
    @jadotati7641 7 หลายเดือนก่อน

    Do a video about java ProcessBuilder please!

  • @robert-qn8hy
    @robert-qn8hy 7 หลายเดือนก่อน

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

  • @StoicismAspects
    @StoicismAspects 7 หลายเดือนก่อน

    Thank you for the video

  • @manish-d2r
    @manish-d2r 7 หลายเดือนก่อน

    nice video Bro

  • @brwashkurismail2556
    @brwashkurismail2556 7 หลายเดือนก่อน

    nothing new, the best as always

  • @kapibara2440
    @kapibara2440 6 หลายเดือนก่อน

    Great content Bro 🎉

  • @이유찬-e5y
    @이유찬-e5y 7 หลายเดือนก่อน

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

    • @aryyann05
      @aryyann05 5 หลายเดือนก่อน

      cls is not a keyword, its just a convention

  • @ishmaelnaga8628
    @ishmaelnaga8628 3 หลายเดือนก่อน

    QUESTION:
    Can't we say that the @classmethod is kind of "useless" as we can replace the "cls" thing with "Student" (class name) itself.Coz i think Student.count would serve the same purpose right ?

  • @goktugmustdie
    @goktugmustdie 3 หลายเดือนก่อน

    i didn't understand the difference between static and class methods completely. can someone enlighten me?

  • @menegideon7351
    @menegideon7351 7 หลายเดือนก่อน

    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💓

  • @theaismith
    @theaismith 7 หลายเดือนก่อน

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

  • @aryyann05
    @aryyann05 5 หลายเดือนก่อน +1

    i love brocode, wanna be my step bro?

  • @SAMUVEL-p5h
    @SAMUVEL-p5h 6 หลายเดือนก่อน

    Bro is in love with sandy💀💀

  • @jayanthkadlur99
    @jayanthkadlur99 5 หลายเดือนก่อน

    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
    b.Enter the task you want to deduct to your list
    c.View you tasks
    d.Stop
    Enter the choice you would like: ")
    if choices == "a":
    add()
    elif choices == "b":
    less()
    elif choices == "c":
    view()
    elif choices == "d":
    print("Byeeeeee!!!")
    break
    else:
    print("INVALID INPUT")

  • @Madhu_-ih4by
    @Madhu_-ih4by 7 หลายเดือนก่อน

    First like First comment 😁😂

  • @mrbaba1900
    @mrbaba1900 7 หลายเดือนก่อน

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

  • @dzwassim95
    @dzwassim95 7 หลายเดือนก่อน

    wtf why there is just a few views