Mar 11, 2013

Python Class and Object Example

Lets discuss the basic Object-Oriented concept of Class & Object in Python with a simple example.

Lets create a Person Class and create objects for the Person class.


Python_Class_Object.py (Copy the below snippet and run the program)

class Person:
    '''Represents a person.'''
    count = 0

    def __init__(self, name):
        '''Initializing data.'''
        self.name = name
        print('\n Initializing %s' % self.name)

        # To count the people
        Person.count += 1

    def displayName(self):
        ''' Saying Hello'''
        print('My name is %s.' % self.name)
    
    def count_people(self):
        '''Prints the count.'''
        if Person.count == 1:
            print('I am the only person here.')
        else:
            print('We have %d persons here.' % Person.count)

Winston = Person('Winston Churchil')
Winston.displayName()
Winston.count_people()

Abraham = Person('Abraham Lincoln')
Abraham.displayName()
Abraham.count_people()
	 



You may also wish to read Object Oriented Concepts in Python as mentioned :
Inheritance in Python

No comments:

Post a Comment