Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Created October 5, 2021 06:41
Show Gist options
  • Save FerdinaKusumah/909c4417f64ee34b6aeb824ae84ed542 to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/909c4417f64ee34b6aeb824ae84ed542 to your computer and use it in GitHub Desktop.
Simple Python Class
class Student:
name: str = "foo"
hobby: str = "bar"
def __init__(self, age: int = 0):
self.age = age
@property
def get_name(self):
return self.name
@property
def get_hobby(self):
return self.hobby
@property
def get_age(self):
return self.age
if __name__ == "__main__":
# initialize student class
s = Student()
# assign value to it
s.name = "Bob"
s.age = 19
s.hobby = "swimming"
# print value inside class as dictionary
# it will print {'age': 19, 'name': 'Bob', 'hobby': 'swimming'}
print(s.__dict__)
# print current value in class
print(s.name) # Bob
print(s.age) # 19
print(s.hobby) # swimming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment