To create a class called Person
in Python, we can follow the syntax:
main.py203 chars11 lines
In the above code, __init__
is the constructor method, which is executed when an object of the class is created. self
is used to represent the instance of the class.
We have defined two methods get_name
and get_age
, which are used to retrieve the values of the name
and age
attributes.
We can create an instance of the Person
class by calling the constructor with the required arguments:
main.py29 chars2 lines
The above code creates an instance of the Person
class with name "John" and age 25. We can then access the attributes of this instance using the methods we defined:
main.py85 chars4 lines
gistlibby LogSnag