Basics of object-oriented programming in Python #1

Take a class named Person as an example. According to the convention, the name of the class should start with a capital letter, and in the case of a name consisting of many words, we use Pascal case – all words written together and beginning with capital letters. The basic definition of a class is as follows:

class Person:

All classes inherit from the base object class by default, so you could define the class as follows:

class Person(object):

Objects can be created from a class that contains only an empty pass statement. Creating an object does not require a special keyword, e.g. new

 john_doe = Person()   # creating an instance of Person class

print(john_doe)   # showing name of an instance and its address

print(dir(john_doe))   # showing all available class methods

To define a class that inherits from another class, put the base class name as an argument, i.e .:

class Employee(Person):

Python supports multi-inheritance, i.e. you can create a class that inherits from multiple base classes, e.g.

class Executive(Employee, Manager):

>>part two<<

Leave a Reply

Your email address will not be published. Required fields are marked *