Python – Classes

Classes create a new object type and new instances of that type are made. Classes are a means of building data and functionality together. Python provides all of the features of object oriented programming: a class inherita

nce mechanism for managing multiple base classes, a derived class can override any method of its base class or classes.

In C++ terminology normally class members are public with the exception of private variables and all member functions are virtual. Classes themselves are objects and provides semantics for importing and naming.

Objects have individuality and multiple names in multiple scopes can be bound to the same object; a practice that is known as aliasing in other languages. Aliasing behaves like pointers in some respects. The pointer is passed by implementation and if the function modifies the objects passed by an argument the caller will notices the change which thus eliminates the need for an argument passing mechanism.

A namespace is mapping from names to objects. Most namespaces are implemented as dictionaries. Essentially, the set of attributes of an object form a namespace. The most important note is that there is no relationship between names in different namespace.

An attribute is a name that follows a dot. So if I create an object call modname and a function called funcname, when I call that function I call modName.functname. In this case funcname is the attribute of the object modname.

A scope is a textual region of Python where a namespace can be accessed directly. During execution there are at least three scopes whose names are directly accessible:

  • the innermost scope which is searched contains the local names
  • the scope of any enclosed functions, which are searched starting with the nearest enclosed scope, contains non-local, but also global names
  • the next to last scope contains the current module’s global names
  • the outermost scope (searched last) is the namespace containing built in names

**This article is written for Python 3.6

<< Exception Handling

Getting Started with the Standard Library >>

%d bloggers like this: