The functions in a class which belong to the object are known as methods
class student_kit():
pass
class student_kit():
principal_name='Mr ABC' #class attributes
Alex=student_kit() # object declaration
In above code we have declared one class attribute as principal_name. All objects have access to this class attribute. So we can print the value of the class attribute by using object.
print(Alex.principal_name)
output is hereMr ABC
While displaying mark sheet of 100 students we don't expect the name of the Principal to change frequently, ( student name changes all the time ) so we used a variable principal_name inside a class. When we use variable inside a class we call it class variable. This class variable is available to all the methods within the class.
Class variables are same as class attributes
class student_kit():
class student_kit():
principal_name='Mr ABC' #class attributes
def attendance(self): # method declaration
print("Is a student")
Alex=student_kit() # object declaration
student_kit.attendance(Alex) # Is a student (output)
Alex.attendance() # Is a student(output)
Read more on instance attributes below. __init__() for this. Every time the object is created this initialization process of executing __init__() is executed.
class student_kit():
college_name='ABC College' #class attributes
#instance attributes
def __init__(self,name,section):
self.name= name
self.section=section
self.height=5 #instance attribute
Alex=student_kit('Alex','A') # object declaration
print(Alex.name,Alex.section,Alex.height) #object accessing instance attributes
Output is here
Alex A 5
In above code the first parameter of the function __init__() is the object itself. We have given self as name of it, you can use any other name but for better understanding it is advisable to use the name self.
print(Alex.__class__.principal_name) # Mr ABC
print(student_kit.principal_name) # Mr ABC
print(Alex.principal_name) # Mr ABC
However we can't access instance attribute as class property
print(student_kit.height) # error
AttributeError: type object 'student_kit' has no attribute 'height' ( what is an AttributeError )
class Animal():
#class attributes
species='carnivores'
#instance attributes
def __init__(self1,name1,age,weight):
self1.name= name1
self1.age=age
self1.weight=weight
self1.height=5 #instance attribute
#instantiate the Animal class
tiger=Animal("Ronald",5,15) # tiger is an object of Animal class
# access the instance attributes
print("{} is {} years old animal".format( tiger.name, tiger.age))
print("{} weight is ={}".format( tiger.name,tiger.weight))
print("{} height is ={}".format( tiger.name,tiger.height))
# object can access the class attributes directly
print("Species is {}".format(tiger.species))
#object can access the class attributes
print("tiger is a {}".format(tiger.__class__.species)) #carnivores
#class can access class attributes
print(Animal.species)
#instant attributes belong to object ( not to class )
print(Animal.height) #AttributeError: type object 'Animal' has no attribute 'height'
class my_class():
my_subject='Python' # class attributes
#instance attributes
def __init__(self,name,mark):
self.name= name
self.mark= mark
def discount(self):
if(self.mark > 50):
return "Discount is avilable"
else:
return "No Discount"
Alex=my_class('Alex',40) # object is defined
print(Alex.discount()) # Discount method is used
print(Alex.my_subject) # Class attribute is used
Output
No Discount
Python
class student_kit():
def register(self,name):# method declaration
self.name=name
print("Welcome:",self.name)
s1=student_kit() # object declaration
s1.register('Alex')
del s1
s1.register('Ron') # NameError
The last line in above code will generate error. We can use try except error handling to manage the error.
Think : How attributes of a method is available for another method ?
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
02-04-2020 | |
| Nice | |