Python booster

This page willgive you a warm feeling in your stomach.

Non-Basic Python features

Theano doesn’t use your grandfather’s python.

  • properties

    a specific attribute that has get and set methods which python automatically invokes.

    See [http://www.python.org/doc/newstyle/ New style classes].

  • static methods vs. class methods vs. instance methods

  • Decorators:

    1. @f
    2. def g():

    runs function f before each invocation of g. See PEP 0318. staticmethod is a specific decorator, since python 2.2

  • metaclass is kinda like a decorator for classes. It runs the metaclass init after the class is defined

  • setattr + getattr + hasattr

  • args is a tuple like argv in C++, *kwargs is a keyword args version

  • pass is no-op.

  • functions (function objects) can have attributes too. This technique is often used to define a function’s error messages.

    1. >>> def f(): return f.a
    2. >>> f.a = 5
    3. >>> f()
    4. 5
  • Warning about mutual imports:

    • script a.py file defined a class A.
    • script a.py imported file b.py
    • file b.py imported a, and instantiated a.A()
    • script a.py instantiated its own A(), and passed it to a function in b.py
    • that function saw its argument as being of type main.A, not a.A.

    Incidentally, this behaviour is one of the big reasons to put autotests in different files from the classes they test!

    If all the test cases were put into <file>.py directly, then during the test cases, all <file>.py classes instantiated by unit tests would have type main.<classname>, instead of type <file>.<classname>. This should never happen under normal usage, and can cause problems (like the one you are/were experiencing).