PYTHON CLASSES By Craig Pennell CLASS DEFINITIONS Like

PYTHON CLASSES By Craig Pennell

CLASS DEFINITIONS Like in C++, class definitions must be called before use. Class statements will usually be function definitions Function definitions will contain arguments and are called using methods. A new namespace is created when the class is defined

CLASS OBJECTS To call an attribute or method from a class, use a format like my. Class. function. Name. For example, My. Class. i = 12345 and My. Class. f will call function f and return ‘hello world’. __doc__ will return the docstring of the class

CLASS OBJECTS The __init__ is a special method that will run when the class is defined with those parameters. Def __init__(self) self. data = 0

INSTANCE OBJECTS Data attributes can be declared at any time For example, the code below produces the value 16 Once defined, they will remain defined until deletion

METHOD OBJECTS In python, method objects can be stored for later use. Note that x. f does not require an argument definition above.

DATA ATTRIBUTES Data attributes can override the method objects of a class. To avoid this, use a naming conventions for data attributes and methods to differentiate them.

INHERITANCE Class objects search the derived class before the base class. Derived classes can override base class methods. However they can still extend methods by calling Base. Class. Name. methodname(self, arguments). isinstance(obj, type) checks if the object is or is derived from that type. issubclass(obj, obj 2) checks inheritance.

MULTIPLE INHERITANCE The above code is used to define multiple bases. Old classes search for attributes from left to right (base 1 then base 2 then base 3) New style classes use the super call, which allows for a more dynamic method of traversal.

PRIVATE VARIABLES There is no such thing as private variables in Python. A method known as name mangling can be used to avoid accidental clashes with names by subclasses. By adding two underscores before an identifier like __i will be textually replaced with _classname__i. Note the variable can still be accessed regardless.

ODDS AND ENDS An empty class can be used the same way struct is used in C.

USER-MADE EXCEPTIONS By using the raise statement, you can make your own exception. You can write it as raise instance or raise Class, instance The code on the left demonstrates this in practice.

ITERATORS A for loop in python uses iterators to function The code shows the implementation of for(‘abc’) it. next() goes to the next item in the list. Stop. Iteration stops the iterator.

ITERATORS Iterators can be extremely useful for classes. The code displayed takes a string and reverses it. Note the need for an _iter_ definition and a next definition.

GENERATOR Generators are a much more compact and readable way to use iterators. Generators automatically creates the skip() and __iter__, so it is much shorter.

GENERATOR EXPRESSIONS Simple generators can be written as generator expressions. Generator expressions are even easier to write. They can be used just like any expression.
- Slides: 16