Sie sind auf Seite 1von 6

Oops

Chapter 5
Concepts of Object Oriented Programming
The basic concepts related to OOP are as follows:
1. Objects
2. Classes
3. Encapsulation
4. Abstraction
5. Data Hiding
6. Polymorphism
7. Inheritance
Built in class attributes

Every Python class keeps the following built-in attributes and they can be accessed using dot operator like any other attribute:

i) __dict__ : It gives the dictionary containing the class's namespace.

ii) __doc__ : It returns the class's documentation string(also called docstring) and if no docstring is defined for a class this built in attribute
returns None

iii) __name__: It gives the class name.

iv) __module__: It specifies the module name in which the class is defined. This attribute is called "__main__" in interactive mode.

v) __bases__ : It gives a possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. (You will
learn about base classes in the next chapter on Inheritance)
Inheritance

Inheritance allows us to define a class that inherits all the methods and properties
from another class.


Parent class is the class being inherited from, also called base class.


Child class is the class that inherits from another class, also called derived class.


Inheritance Cont..

Create a Parent Class

Any class can be a parent class, so the syntax is the same as creating any other
class:

Create a class named Person, with firstname and lastname properties, and a
printname method:


class Person:

def __init__(self, fname, lname):

self.firstname = fname

self.lastname = lname

def printname(self):

print(self.firstname, self.lastname)


x = Person("John", "Doe")
Inheritance Cont..

Create a Child Class

To create a class that inherits the functionality from another class, send the
parent class as a parameter when creating the child class:

Create a class named Student, which will inherit the properties and methods from
the Person class:


class Student(Person):

pass # Note: Use the pass keyword when you do not want to add any other properties or methods to
the class.


Use the Student class to create an object, and then execute the printname method:


x = Student("Mike", "Olsen")

x.printname()

Das könnte Ihnen auch gefallen