Creating Classes and Objects in Python
* One can create a class "Person" in which
"Andy" and "Bandy" are the names of the objects within the
class
* A Class can be created by using the keyword "class" infront of the name of the class that one is trying to make .
* A Class describes the attributes and actions performed by the objects of the class . Therefore , one can write the attributes ( Variables ) and actions ( Functions ) in the class as :
# This is a Class with the name Person attributes means
variables
Class Person:
name = 'Andy'
age = 20
# actions means functions
def talk(cls):
print(cls.name)
print(cls.age)
* One can observe from the given code that the Person Class has
two variables and one function . The function that is written in the class is
called a Method .
* If someone wants to use this Class , then one should create an
Object for the class first in the given manner .
def talk(cls):
Here , 'cls' represents a default parameter that indicates the class of the object . Therefore , cls.name refers to the class variable 'Andy' . One can call the "talk()" method to display the object's details as :
p1.talk()
===========================================================
No comments:
Post a Comment