Created
March 13, 2023 06:17
-
-
Save thetongs/c37fbf6fbf5dcc35a54859467ece1aa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student: | |
# class variables | |
school_name = 'ABC School' | |
def __init__(self, name, age): | |
self.name = name | |
self.age = age | |
# instance method | |
def show(self): | |
# access instance variables | |
print('Student:', self.name, self.age) | |
# access class variables | |
print('School:', self.school_name) | |
@classmethod | |
def change_School(cls, name): | |
# access class variable | |
print('Previous School name:', cls.school_name) | |
cls.school_name = name | |
print('School name changed to', Student.school_name) | |
@staticmethod | |
def find_notes(): | |
# can't access instance or class attributes | |
return ['chapter 1', 'chapter 2', 'chapter 3'] | |
# create object | |
jessa = Student('Jessa', 12) | |
# call instance method | |
jessa.show() | |
# call class method | |
Student.change_School('XYZ School') | |
# call static method | |
jessa.find_notes() | |
# Student: Jessa 12 | |
# School: ABC School | |
# Previous School name: ABC School | |
# School name changed to XYZ School | |
# ['chapter 1', 'chapter 2', 'chapter 3'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment