Tutorial by Examples

Inheritance in Python is based on similar ideas used in other object oriented languages like Java, C++ etc. A new class can be derived from an existing class as follows. class BaseClass(object): pass class DerivedClass(BaseClass): pass The BaseClass is the already existing (parent)...
Instance variables are unique for each instance, while class variables are shared by all instances. class C: x = 2 # class variable def __init__(self, y): self.y = y # instance variable C.x # 2 C.y # AttributeError: type object 'C' has no attribute 'y' c1 = C(3) c1....
The idea of bound and unbound methods was removed in Python 3. In Python 3 when you declare a method within a class, you are using a def keyword, thus creating a function object. This is a regular function, and the surrounding class works as its namespace. In the following example we declare method ...
Python 2.x2.2.0 New-style classes were introduced in Python 2.2 to unify classes and types. They inherit from the top-level object type. A new-style class is a user-defined type, and is very similar to built-in types. # new-style class class New(object): pass # new-style instance new = N...
If the variable contains a value of an immutable type (e.g. a string) then it is okay to assign a default value like this class Rectangle(object): def __init__(self, width, height, color='blue'): self.width = width self.height = height self.color = color d...
Python uses the C3 linearization algorithm to determine the order in which to resolve class attributes, including methods. This is known as the Method Resolution Order (MRO). Here's a simple example: class Foo(object): foo = 'attr foo of Foo' class Bar(object): foo = 'attr foo o...
Descriptors are objects that are (usually) attributes of classes and that have any of __get__, __set__, or __delete__ special methods. Data Descriptors have any of __set__, or __delete__ These can control the dotted lookup on an instance, and are used to implement functions, staticmethod, classmet...
Class methods present alternate ways to build instances of classes. To illustrate, let's look at an example. Let's suppose we have a relatively simple Person class: class Person(object): def __init__(self, first_name, last_name, age): self.first_name = first_name self.last...
Class composition allows explicit relations between objects. In this example, people live in cities that belong to countries. Composition allows people to access the number of all people living in their country: class Country(object): def __init__(self): self.cities=[] ...
In this case, "monkey patching" means adding a new variable or method to a class after it's been defined. For instance, say we defined class A as class A(object): def __init__(self, num): self.num = num def __add__(self, other): return A(self.num + other.num)...
The dir() function can be used to get a list of the members of a class: dir(Class) For example: >>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__...
A class, functions as a template that defines the basic characteristics of a particular object. Here's an example: class Person(object): """A simple class.""" # docstring species = "Homo Sapiens" ...
Python classes support properties, which look like regular object variables, but with the possibility of attaching custom behavior and documentation. class MyClass(object): def __init__(self): self._my_string = "" @property def string(self): "&...
A singleton is a pattern that restricts the instantiation of a class to one instance/object. For more info on python singleton design patterns, see here. class Singleton: def __new__(cls): try: it = cls.__it__ except AttributeError: it = cls.__it__ =...

Page 1 of 1