This is the last tutorial in the Python Tutorials for Beginners. This Python beginner tutorial explains Python classes and Python objects. Please view the Python video tutorial 19 or read on... What is class in Python programming? A class is a template or a pattern to create objects. Python classes support object-oriented programming. A Python class has class attributes. Also, a Python class can be updated after it's definition. The objects created from a Python class are called instance objects. The Python class syntax is:
class ClassName:
class body
class ClassName:
class body
In the above class format, the Python keyword class is followed by the name of the class, then a colon and then one or more Python statements. Now, let us see Python class examples.
# Python code
class Tree:
"""A class that represents a tree""" # doc string - optional
species = 'Pine' # class attribute - data attribute
def describe_me (self): # class attribute - class method
return 'this is a tree'
# print the data attribute of the Tree class i.e. print 'Pine'
print (Tree.species)
# print the documentation string of the Tree class
print (Tree.__doc__)
# update the Python class (after class definition)
Tree.species = 'Aspen'
print (Tree.species)
# Python code
class Tree:
"""A class that represents a tree""" # doc string - optional
species = 'Pine' # class attribute - data attribute
def describe_me (self): # class attribute - class method
return 'this is a tree'
# print the data attribute of the Tree class i.e. print 'Pine'
print (Tree.species)
# print the documentation string of the Tree class
print (Tree.__doc__)
# update the Python class (after class definition)
Tree.species = 'Aspen'
print (Tree.species)
# class instantiation i.e. create objects
tree1 = Tree()
# note that there is no need to provide the self argument when calling the class method, describe_me()
print (tree1.describe_me(), 'of species', tree1.species)
# find out if tree1 is an object of the class Tree or not
print (isinstance(tree1, Tree))
# Python examples
class Phone:
# class body starts
family = 'Electronics' # family is a class attribute and also a data attribute
# __init__ is a class attribute, class method and a constructor
def __init__ (self, customer): # self means the current instance object
self.owner = customer
# class body ends
# Python code to create objects by calling the name of the class, which calls __init__ class method
# note that self is not provided as an argument, only the customer argument is needed
p1 = Phone ('John')
p2 = Phone ('Jane')
# Python code to use class variable, family (shared by all objects of the class)
# print the family of both p1 and p2 objects i.e. print 'Electronics'
print ('p1 object belongs to the family,', p1.family)
print ('p2 object belongs to the family,', p2.family)
# Python code to use instance variable, owner (unique to each object of the class)
# print the owner of p1 object i.e. print 'John'
print ('p1 object is owned by', p1.owner)
# print the owner of p2 object i.e. print 'Jane'
print ('p2 object is owned by', p2.owner)
# Python code to add custom data attributes to the Phone objects
# add data attribute, new to p1 object
p1.new = 'No'
print ('Is p1 object new?', p1.new)
# add two data attributes, boxed and rear_cameras to p2 object
p2.boxed = 'Yes'
p2.rear_cameras = 4
tree1 = Tree()
# note that there is no need to provide the self argument when calling the class method, describe_me()
print (tree1.describe_me(), 'of species', tree1.species)
# find out if tree1 is an object of the class Tree or not
print (isinstance(tree1, Tree))
# Python examples
class Phone:
# class body starts
family = 'Electronics' # family is a class attribute and also a data attribute
# __init__ is a class attribute, class method and a constructor
def __init__ (self, customer): # self means the current instance object
self.owner = customer
# class body ends
# Python code to create objects by calling the name of the class, which calls __init__ class method
# note that self is not provided as an argument, only the customer argument is needed
p1 = Phone ('John')
p2 = Phone ('Jane')
# Python code to use class variable, family (shared by all objects of the class)
# print the family of both p1 and p2 objects i.e. print 'Electronics'
print ('p1 object belongs to the family,', p1.family)
print ('p2 object belongs to the family,', p2.family)
# Python code to use instance variable, owner (unique to each object of the class)
# print the owner of p1 object i.e. print 'John'
print ('p1 object is owned by', p1.owner)
# print the owner of p2 object i.e. print 'Jane'
print ('p2 object is owned by', p2.owner)
# Python code to add custom data attributes to the Phone objects
# add data attribute, new to p1 object
p1.new = 'No'
print ('Is p1 object new?', p1.new)
# add two data attributes, boxed and rear_cameras to p2 object
p2.boxed = 'Yes'
p2.rear_cameras = 4
print ('Is p2 object still boxed?', p2.boxed)
Want to learn with more Python samples? Please view my Python video tutorial 19. Thank you.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.