0% found this document useful (0 votes)
90 views

Python For Programmers - Object-Oriented Programming in Python Cheatsheet - Codecademy

Polymorphism allows objects of different types to share a common interface. Objects can then maintain unique functionality through subclassing while sharing the base interface. In Python, polymorphism is demonstrated through classes that inherit behaviors from a parent class while allowing subclasses to override methods. Class variables are defined outside methods and have the same value for all class instances, while instance variables can hold unique values for each object.

Uploaded by

OUMAIMA BAHAFID
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Python For Programmers - Object-Oriented Programming in Python Cheatsheet - Codecademy

Polymorphism allows objects of different types to share a common interface. Objects can then maintain unique functionality through subclassing while sharing the base interface. In Python, polymorphism is demonstrated through classes that inherit behaviors from a parent class while allowing subclasses to override methods. Class variables are defined outside methods and have the same value for all class instances, while instance variables can hold unique values for each object.

Uploaded by

OUMAIMA BAHAFID
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Python for Programmers

Object-Oriented Programming in
Python

C# Polyphormism
Polymorphism is the ability in programming to present class Novel : Book
the same interface for different underlying forms (data
{
types).
We can break the idea into two related concepts. A public override string Stringify()
programming language supports polymorphism if: {
1. Objects of different types have a common
return "This is a Novel!;
interface (interface in the general meaning, not
just a C# interface), and }
2. The objects can maintain functionality unique }
to their data type

class Book
{
public virtual string Stringify()
{
return "This is a Book!;
}
}

// In the below code, you’ll see that a


Novel and Book object can both be
referred to as Books. This is one of
their shared interfaces. At the same
time, they are different data types with
unique functionality.

Book bk = new Book();


Book warAndPeace = new Novel();
Console.WriteLine(bk.Stringify());
Console.WriteLine(warAndPeace.Stringify()
);

// This is a Book!
// This is a Novel

// Even though bk and warAndPeace are the


same type of reference, their behavior is
different. Novel overrides the
Stringify() method, so all Novel objects
(regardless of reference type) will use
that method.

Object Oriented Programming (OOP)


Object-Oriented Programming (OOP) is a design
paradigm that organizes code into separate objects that
interact with each other. OOP has four primary aspects:
encapsulation, abstraction, inheritance, and
polymorphism. Each plays a role in making a software
system adaptable to change.

Polymorphism in OOP
In object-oriented programming, polymorphism is the
ability to access the behavior of multiple subclasses
through the shared interface of a superclass.

Python class
In Python, a class is a template for a data type. A class # Defining a class
can be defined using the class keyword.
class Animal:
def __init__(self, name,
number_of_legs):
self.name = name
self.number_of_legs = number_of_legs

Python init method


In Python, the .__init__() method is used to initialize class Animal:
a newly created object. It is called every time the class
def __init__(self, voice):
is instantiated.
self.voice = voice

# When a class instance is created, the


instance variable
# 'voice' is created and set to the input
value.
cat = Animal('Meow')
print(cat.voice) # Output: Meow

dog = Animal('Woof')
print(dog.voice) # Output: Woof
Python Class Variables
In Python, class variables are defined outside of all class my_class:
methods and have the same value for every instance of
class_variable = "I am a Class
the class.
Class variables are accessed with the Variable!"
instance.variable or class_name.variable syntaxes.
x = my_class()
y = my_class()

print(x.class_variable) #I am a Class
Variable!
print(y.class_variable) #I am a Class
Variable!

You might also like