Skip to content

Python Beyond the Basics

  • Python
Tags:

Entities

  • class: blueprint for an instance
  • instance: a constructed object of the class
  • type: indicates the class/instance belongs to
  • attribute: value of any object
  • method: a callable attribute defined in the class

Instance Methods

  • instance methods are variables define in the class
  • when call through the instance, the instance is automatically passed as first argument (named self) to the method, e.g. below:
  • because of this automatic passing of the instance, instance method are known as “bound” methods, i.e. bound to the instance upon which it is called

Encapsulation

  • breaking encapsulation: setting the object value without using the setter method

 

  • enforce encapsulation:

Python Class Variable v.s. Instance Attribute

Set class variable and instance attribute the same name to show the difference and look up order:

The attribute look up order: when request an attribute to an instance, it looks for that attribute in the instance first then in the class.

Access class variable in instance:

Polymorphism

  • Different classes has same interface (i.e., method name), we can say that this group of related classes implement the same action
  • Being able to work with different types to call methods without checking it’s possible and handle errors using exception handling is called DOCK TYPING
  • Checking the type ahead of time is generally considered un-pythonic

An example from stackoverflow:

or an built in len method as an interface implemented by list and set object:

Inheriting the Constructor

  • __init__ can be inherited
  • if a class doesn’t have an __init__ constructor, Python will check its parent class, as soon as it finds one, Python calls it and stops looking
  • we can use the super() function to call method in the parents class, or we may want to initialize in the parent class

Depth-First Order

Multiple inheritance

After Python 2.3, add a role for diamond shape ambiguous inheritance

(Make C inherit A in this example)

Abstract Class

  • An abstract class is not designed to and can’t not construct instances, but subclassed by regular classes
  • An interface / methods that defined in abstract class must be implement by its subclass
  • The python abc module enables the creation of abstract class

Composition

  • Inheritance could be brittle (a change may require changes elsewhere)
  • Decoupled code could work independently or interactively
  • classes interactions will work with an interface
  • Not checking types: polymorphic and Pythonic

Here is an example for inheritance and composition working together:

Magic Functions

Attribute Encapsulation

  • @property should not encapsulate expensive operations, because attribute setting looks cheap
  • @property only controls attributes that are expected

“with” in Python

Custom Exception

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *