Python

All about Python Classes – Demo with examples

Python Classes are all types –

print( ' The type of 1 is: ', type(1)) print ( ' The type of [] is:', type([])) A_class = type('A class' , () , {} ) print('The type of A_class is :' , type(A_class)) an_inst = A_class() print('The type of an_inst is: ' , type(an_inst))
class Basic(): start = 1 def a_method(self) : return 'This is an instance of ' + str (self.__class__) basic_inst = Basic() print('The type of Basic is:' , type(Basic)) print('The type of basic_inst is:',type(basic_inst)) print('Calling a_method returns:',basic_inst.a_method())

Class Definitions

''' Demonstrate class defnitions ''' class Base_Model(): '''Represent the base model of a car ''' trim = 'normal' engine_liters = 1.5 def engine_sound(self): return 'putt, putt' def horn_sound(self): return 'beep, beep' def __str__(self): return 'Base Model' coop = Base_Model() print('%s has %s trim level.' % (coop, coop.trim)) print('%s has a %s liter engine.' % (coop, coop.engine_liters)) print('%s engine sounds like %s.' % (coop, coop.engine_sound())) print('%s horn sounds like %s.' % (coop, coop.horn_sound()))

Class Initialization

''' Demonstrate class initialization ''' import locale import sys class Base_Model() : ''' Represent the base model of a car ''' trim = 'normal' engine_liters = 1.5 def __init__(self, price, transmission='automatic', color='white'): self.price = price self.transmission = transmission self.color = color def info(self): if sys.platform.startswith('win'): locale.setlocale(locale.LC_ALL, 'us') else: locale.setlocale(locale.LC_ALL, 'en_US.utf8') print('The price of %s was %s.' % (self, locale.currency(self.price))) def __str__(self): return 'a %s base model with %s transmission' % (self.color, self.transmission) coop = Base_Model(25000) coop.info() coop_blue = Base_Model(price=25500,color='blue') coop_blue.info() coop_manual_red = Base_Model(26000, 'manual', 'red') coop_manual_red.info() coop_manual_green = Base_Model(color='green' , transmission='manual', price=26300) coop_manual_green.info()

Class Methods

''' Demonstrate class methods ''' import locale import sys class Base_Model(): ''' Represent the base model of a car ''' trim = 'normal' engine_liters = 1.5 miles_range = 450 tank_capacity = 45 color = 'white' transmission = 'automatic' @classmethod def miles_per_liter(cls) : return cls.miles_range / cls.tank_capacity @classmethod def miles_per_gallon(cls) : return cls.miles_per_liter() * 3.78541 def __init__(self, price, transmission='automatic' , color='white' ): self.price = price self.transmission = transmission self.color = color def info(self): if sys.platform.startswith('win'): locale.setlocale(locale.LC_ALL , 'us') else: locale.setlocale(locale.LC_ALL , 'en_US.utf8 ') print('The price of %s was %s. ' %(self, locale.currency(self.price))) def __str__(self): return 'a %s base model with %s transmission' %(self.color, self.transmission) coop = Base_Model(color='green' , transmission='automatic', price=25000) coop.info() print('The %s gets %4.1f miles per gallon' % (coop, coop.miles_per_gallon())) print('The %s gets %4.1f miles per gallon' % (Base_Model , Base_Model.miles_per_gallon())) class Sport_Model(Base_Model) : ''' Represent the sport model of a car based on a base mode ''' engine_liters = 2.0 miles_range = 400 coop_sport = Sport_Model(color='green' , transmission='manual', price=26300) coop_sport.info() print('The %s gets %4.1f miles per gallon' % (coop_sport, coop_sport.miles_per_gallon())) print('The %s gets %4.1f miles per gallon' % (Sport_Model , Sport_Model.miles_per_gallon()))

Share This Post

An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.24tutorials.com/sai

Lost Password

Register

24 Tutorials