Python For Programmers - Object-Oriented Programming in Python Cheatsheet - Codecademy
Python For Programmers - Object-Oriented Programming in Python Cheatsheet - Codecademy
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!;
}
}
// This is a Book!
// This is a Novel
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
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!