Show Menu
Cheatography

Python Advanced Cheat Sheet by

Python Advanced Concepts

Object

Every real-world entity is an object.An object has Behaviour (things it does or performs) and Attributes (things that describe it).

For eg: A Chair object can have behaviour like Movement, Height Adjustment & Attributes like Color, Make & Model, and Price.

Encaps­ulation

It means wrapping data into a single unit & securing it.

For eg: Drug Capsule wraps different medicines into a single unit and protects them from the outside enviro­nment.
Bank Locker wraps your valuables into a single unit(l­ocker) and protects it via passcode.

Abstract class/­Method

Abstract class is a class that cannot be instan­tiated. However, you can create classes that inherit from an abstract class. An abstract method is an method without an implem­ent­ation. An abstract class may or may not include abstract methods.

Python doesn’t directly support abstract classes. But it does offer a module that allows you to define abstract classes.To define an abstract class, you use the abc (abstract base class) module.

Ex:
from abc import ABC, abstra­ctm­ethod
class Polygo­n(ABC):
@abstr­act­method
def noofsi­des­(self):
pass
 

Class

The collection of all related objects is called a class.C­on­sider class as a general category which contains all the related objects inside it.

For eg: Objects like Wheelc­hair, Office Chair and Wooden Chair can be a part of the "­Cha­ir" class.

Abstra­ction

Hiding complexity from the user and showing only the relative stuff.

For Eg: In Car, all the complexity like the engine, machinery, etc is hidden from you; only relevant parts are shown, like the brakes, accele­rator, and gearbox.

Generators

Generators are functions that return an iterable generator object. Because the values from the generator object are fetched one at a time rather than the entire list at once, you can use a for-loop, next(), or list() function to get the actual values. Generator functions act just like regular functions with just one difference that they use the Python yield keyword instead of return .

Code:

def test_s­equ­ence():
num = 0
while num<10:
yield num
num += 1
for i in test_s­equ­ence():
print(i, end=",")

Output: 0,1,2,­3,4­,5,­6,7,8,9
 

Inheri­tance

The way we inherited a few qualities from our parents similarly, a class can also inherit the qualities from a parent class.

For eg: A Phone Class can have two Child Classes: 1) TelePhone
and 2) Mobile­Phone. Both can inherit the "­cal­lin­g" behaviour.

Different types of Inheri­tance:
 ­ ­ ­ Single inheri­tance: When a child class inherits from only one parent class, it is called single inheri­tance. We saw an example above.
 ­ ­ ­ Multiple inheri­tances: When a child class inherits from multiple parent classes, it is called multiple inheri­tances.
 ­ ­ ­ Multilevel inheri­tance: When we have a child and grandchild relati­onship.
 ­ ­ ­ Hierar­chical inheri­tance: More than one derived class are created from a single base.
 ­ ­ ­ Hybrid inheri­tance: This form combines more than one form of inheri­tance. Basically, it is a blend of more than one type of inheri­tance.

Polymo­rphism

It means many forms. With the same name, it provides different forms.

For eg: In Chess, we've 6 pieces - king, rook, bishop, queen, knight, and pawn. All of them "­mov­e" differ­ently i.e. Bishop moves diagon­ally, Rooks move horizo­ntally and vertic­ally, etc.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          oAuth End Points Cheat Sheet
          Object Oriented Design Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet