Skip to content

Instantly share code, notes, and snippets.

@ivalexandru
Created February 23, 2018 15:32
Show Gist options
  • Save ivalexandru/e19758f93b8e86890e3b283007f87820 to your computer and use it in GitHub Desktop.
Save ivalexandru/e19758f93b8e86890e3b283007f87820 to your computer and use it in GitHub Desktop.
#class = a template from which objects can be created
#all the obj created from the same class will share the same caracteristici
#an instance is just another name created from a class definition
"""Planul unei case descrie doar cum va arata asta, cate camere va avea; ca si clasa
odata ce ai un plan, poti construi cate case vrei"""
"""When a variable is bound to an instance of a class, it's refered to as a data attribute"""
""" CONSTRUCTOR = a special method that is executed when an instance of a class is created or constructed
the __init__ method de mai jos"""
class Kettle(object):
power_source = "electricity"
def __init__(self, make, price):
self.make = make
self.price = price
self.on = False
#cand vom crea obj in aceasta clasa, ele vor avea un nume si un pret
#nu acelasi nume si acelasi pret, fiecare instanta a clasei avand val diferite
def switch_on(self):
self.on = True
#the self parameter e cam obligatoriu, e o conventie
#unele tooluri nu merg daca nu pui self acolo
#self e o referenta catre instanta clasei
kenwood = Kettle("kenwood", 8.99) #creates an instance of the Kettle class
#astia doi sunt param de sus
#make si price
print(kenwood.make) #kenwood
print(kenwood.price) #8.99
#la print nu mai tre sa pui nicaieri Kettle
kenwood.price = 12.75
print(kenwood.price) #12.75
hamilton = Kettle("hamilton", 14.55)
salam = Kettle("salam", 99999)
print("Models: {} = {}, {} = {}".format(kenwood.make, kenwood.price, salam.make, salam.price))
#poti specifica atributele obiectelor in replacements fields:
print("Models: {0.make} = {0.price}, {1.make} = {1.price}".format(kenwood, hamilton))
print(hamilton.on) #False
hamilton.switch_on() #aici nu mai tre sa pui self in parant
#we've never have to specify an argument for the self parameter
#because py supplies the value automaticly when calling the method
print(hamilton.on) #True, ca l-ai pornit mai sus
#o alta metoda de switch on:
#daca folosesti medota in care pui si numele clasei, cere parametru intre parantze
Kettle.switch_on(kenwood)
print(kenwood.on)
kenwood.switch_on()
print("=" * 33)
#we add another data atribute, called power, to the kenwood instance of the kettle class
#power = instance variable, adica o variabla bound to an instance of the Kettle class.
kenwood.power = 1.5
print(kenwood.power)
#print(hamilton.power)
print("Switch to atomic power")
Kettle.power_source = 'atomic' #schimba din electricity in atomic
#the two instances are sharing the attribute existent in the class
print(Kettle.power_source) #electricity toate 3
print(kenwood.power_source)
print(hamilton.power_source)
print(Kettle.__dict__)
print(kenwood.__dict__)
print(hamilton.__dict__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment