1 / 16

Python Classes

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.

rgish
Download Presentation

Python Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Classes By Craig Pennell

  2. 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

  3. 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

  4. 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

  5. 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

  6. Method Objects • In python, method objects can be stored for later use. • Note that x.f does not require an argument definition above.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. Odds and Ends • An empty class can be used the same way struct is used in C.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

More Related