170 likes | 201 Views
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.
E N D
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 myClass.functionName. • For example, MyClass.i = 12345 and MyClass.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 BaseClassName.methodname(self, arguments). • isinstance(obj,type) checks if the object is or is derived from that type. • issubclass(obj,obj2) checks inheritance.
Multiple Inheritance • The above code is used to define multiple bases. • Old classes search for attributes from left to right (base1 then base2 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 asraise instanceorraise 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. • StopIteration 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.