Python Objects

Introduction

Everything in Python is an object.

Import

The Import Search Path

Python looks in all the directories defined in sys.path when you try to import a module.

Import modules

  1. import module
  2. from module import

difference: the attributes and methods of the imported module types are imported directly into the local namespace, so they are available directly, without qualification by module name.

Example

>>> import types
>>> types.FunctionType             #1
<type 'function'>
>>> FunctionType                  #2
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
NameError: There is no variable named 'FunctionType'
>>> from types import FunctionType #3
>>> FunctionType                   #4
<type 'function'>
  1. The types module contains no methods; it just has attributes for each Python object type. Note that the attribute, FunctionType, must be qualified by the module name, types.
  2. FunctionType by itself has not been defined in this namespace; it exists only in the context of types.
  3. This syntax imports the attribute FunctionType from the types module directly into the local namespace.

Classes

  • Python’s class mechanism adds classes to the language with a minimum of new syntax and semantics.
  • All class members are public, and all member functions are virtual.
  • In Python, all data types are objects.

Form of class definition:

class ClassName: 
  statement-1 
  ...
  statement-N
  • Class definitions, must be executed before they have any effects.
  • When a class definition is entered, a new namespace is created, and used as the local scope.
  • When a class definition is left normally (via the end), a class object is created.

Class Objects

  • Class objects support two kinds of operations: attribute references and instantiation.
  • Attribute references use the standard syntax used for all attribute references in Python: obj.name.
  • Class instantiation uses function notation.

x = MyClass()
Creates a new instance of the class and assigns this object to the local variable x.

Instance Objects

  • The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names, data attributes and methods.
  • data attributes correspond to “instance variables”

*A method is a function that “belongs to” an object.

Method Objects

  • The object is passed as the first argument of the function.

Inheritance

Introduction

  • The syntax for a derived class definition is the following:
class DerivedClassName(BaseClassName):
    <statement-1>
    ...
    <statement-N>
  • Derived classes may override methods of their base classes.

Multiple Inheritance

  • Python supports a limited form of multiple inheritance.
  • For old-style classes, the only rule is depth-first, left-to-right.
  • For new-style classes, the method resolution order changes dynamically to support cooperative calls to super().

Private Variables

  • There is limited support for class-private identifiers. Any identifier of the form spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classnamespam.

Odds and Ends

  • Empty class definition: Similar to a C "struct".
class Employee:
    pass
 
john = Employee() # Create an empty employee record
 
# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

Exceptions

  • User-defined exceptions are identified by classes as well.

Iterators

  • The for statement calls iter() on the container object. The function returns an iterator object that defines the method next() which accesses elements in the container one at a time. When there are no more elements, next() raises a StopIteration exception which tells the for loop to terminate.

Generators

  • Generators are tools for creating iterators.
  • They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called, the generator resumes where it left-off.

Object Attributes

name

Modules are objects, and all modules have a built-in attribute name. A module's name depends on how you're using the module. If you import the module, then name is the module's filename, without a directory path or file extension. But you can also run the module directly as a standalone program, in which case name will be a special default value, main.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.