#----------------------------- 1 - creating a Class ---------------------------------
# syntax : class class_name:
class news: # class property
sent ="Today no special news"
#----------------------------- 2 - creating a Object for class------------------------
# syntax: object_name.class_name() , to invoke the class
class news: # class property
sent ="Today no special news" # property
obj = news() # object
print(obj.sent)# accessing the property of the class
#----------------------------- 3 - __init__() function in a class--------------------
class blog:
def __init__(self, name, year): # __init__() s an built in function
#__init__() function is used to assign values to object properties
# when __init__()
is created, the class is called when new object is created
self.name = name
self.year = year
post = blog("Python", 2023) # assigning class to variable post
print(post.name)
print(post.year)
#----------------------------- 4 - __str__() function in a class--------------------
class blog:
def __init__(self, name, year, duration): # __init__() s an built in function
#__init__() function is used to assign values to object properties
self.name = name
self.year = year
self.duration =duration
def __str__(self):
return f"{self.name} - {self.year} - {self.duration}"
post = blog("Python", 2023,'1 month') # assigning class to variable post
print(post) # automatically invoke the class
#----------------------------- 5 - calling function/method in a class----------------
# syntax: object_name.function_name() , to invoke the function
class blog:
def __init__(self, name, year, duration): # __init__() s an built in function
#__init__() function is used to assign values to object properties
self.name = name
self.year = year
self.duration =duration
def course(self): # defining a function
print("We provide easy way of learning for",self.name," in ",self.duration)
post = blog("Python", 2023,'1 month.') # assigning class to variable post
post.course() # function is called from the class
#----------------------------- 6 - Updating values in a class --------------------
class blog:
def __init__(self, name, year, duration): # __init__() s an built in function
#__init__() function is used to assign values to object properties
self.name = name
self.year = year
self.duration =duration
def course(self):
print("We provide easy way of learning for",self.name," in ",self.duration)
post = blog("HTML", 2023,'1 month.') # assigning class to variable post
post.year = 2022
post.duration='15 days'
post.course() # function is invoked from the class
#----------------------------- 7 - deleting values in a class --------------------
class blog:
def __init__(self, name, year, duration): # __init__() s an built in function
#__init__() function is used to assign values to object properties
self.name = name
self.year = year
self.duration =duration
def __str__(self):
return f"{self.name} - {self.year} - {self.duration}"
post = blog("Python", 2023,'1 month') # assigning class to variable post
print(post) # automatically invoke the class
# to delete an element in a class 'del' keyword is used
# Syntax : del object_name.function_name() / del object_name.variable()
del post.duration # will delete duration attribute from the class
print(post) # will return a error because duration has been deleted so it can't be displayed