Class:template for creating objects.All objects created using the same class will have the same charecteristics
Objects:an instance of a class
Instantiate:Create a Instance of a class
Method:a funtion defined in a class
Attribute:a variable bound to a instance of a class
#everything is objects in python
#take away : class and objects
# a = 1
# b = 3
# print (a + b)
# print (a.__add__(b))
#so + is same as __add__ ,if u ctrl right click it will goto the same definition
class kettle (object):
power_source = 'fuel'
def __init__(self,make,price):
self.make = make
self.price = price
self.on = False
def switch_on(self):
self.on = True
kenwoodobj = kettle('kenwood',25)
hamilton = kettle('hamilton',30)
print(kenwoodobj.make)
print(hamilton.make)
print(hamilton.price)
hamilton.make = 'Hamiltoon'
print(hamilton.make)
print(kenwoodobj)
print ('{}={},{}={}'.format(kenwoodobj.make,kenwoodobj.price,hamilton.make,hamilton.price))
kenwoodobj1 = kenwoodobj
print(id(kenwoodobj1))
print(id(kenwoodobj))
print(id(hamilton))
print(id(hamilton.make))
#we can call the object by
#classname.objectname(instancename)
#or
#instancename.objectname()
kettle.switch_on(hamilton)
print(hamilton.make)
print(hamilton.on)
hamilton.on = False
print(hamilton.on)
hamilton.switch_on()
print(hamilton.on)
#this is called as instance variable,variable is only for this instance,not for the entire instances of the
#class
kenwoodobj.power = 1.5
print(kenwoodobj.power)
#if we try accessing the same variable for different instance,it will throw error
# print(hamilton.power)
print (kettle.power_source)
print (kenwoodobj.power_source)
print (hamilton.power_source)
print(kettle.__dict__)
print(kenwoodobj.__dict__)
print(hamilton.__dict__)
print ('*' * 80)
# kettle.power_source = 'atomic'
print (kettle.power_source)
print (kenwoodobj.power_source)
print (hamilton.power_source)
print(kettle.__dict__)
print(kenwoodobj.__dict__)
print(hamilton.__dict__)
print ('/' * 80)
#if we assign the value for the calss attribute instance,it becomes instance vriable and remains
#unaffected for the class and other instances
kenwoodobj.power_source = 'gas'
print (kettle.power_source)
print (kenwoodobj.power_source)
print (hamilton.power_source)
print(kettle.__dict__)
print(kenwoodobj.__dict__)
print(hamilton.__dict__)
print ('*' * 80)
hamilton.power_source = 'fossil fuel'
print (kettle.power_source)
print (kenwoodobj.power_source)
print (hamilton.power_source)
print(kettle.__dict__)
print(kenwoodobj.__dict__)
print(hamilton.__dict__)
hamilton.power_source = 'fuel'
print (kettle.power_source)
print (kenwoodobj.power_source)
print (hamilton.power_source)
print(kettle.__dict__)
print(kenwoodobj.__dict__)
print(hamilton.__dict__)
print ('@' * 80)
kettle.power_source = 'fuel'
print (kettle.power_source)
print (kenwoodobj.power_source)
print (hamilton.power_source)
print(kettle.__dict__)
print(kenwoodobj.__dict__)
print(hamilton.__dict__)
this will explain how a basic program document has to be written
#this will explain you how to document a program(DocString)
class song:
"""
class to represent a song
Attributes:
title():The tile of the song
artist(Artist):An artist object representing the songs creator
duration (int):The duration of the song in seconds.May be Zero
"""
def __int__(self,title,artist,duration=0):
"""song init method
Args:
title(str):Intialises the 'title' attribute
artist(Artist):An artist object representing the songs creator
duration (optional int):initialise value to 'duration' attribute
will be defaulted to zero if not specified.
"""
self.title = title
self.artist = artist
self.duration = duration
No comments:
Post a Comment