Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Learn Python CLASS METHODS in 6 minutes! ๐Ÿซ
Play lesson

Python tutorial for beginners ๐Ÿ - Learn Python CLASS METHODS in 6 minutes! ๐Ÿซ

4.0 (0)
8 learners

What you'll learn

This course includes

  • 14.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

# 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())

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere