8/22/2014

Python Decorators

Python Decorators

What are Python decorators?

Python decorators are special syntax of the language that allows for functions to be applied to other functions or classes. The Python Enhancement Proposal (PEP) 318 describes the syntax used by these decorators as applied to functions. The Python Enhancement Proposal (PEP) 3129 describes the syntax used by these decorators as applied to classes.

The first use of decorator notation of @function_name appeared in Python 2.2.  The decorators for class methods and static functions were created so instead of using the following for a classmethod or a staticmethod: 

class Foo(object): 
    def bar(cls):
           pass
    bar = classmethod(bar)

class Biz(object):
    def baz():
        pass
    baz = staticmethod(baz)

The new decorator syntax allows you to call the __builtins__ functions, classmethod() and staticmethod() by using an @function_name syntax. This applied either the classmethod or staticmethod built-in functions to the function that followed the @function_name syntax.

class Foo(object): 
    @classmethod
    def bar(cls):
           pass

class Biz(object):
    @staticmethod
    def baz():
        pass

PEP 318 also proposed and extended this syntax to be able to be used by user defined functions.  PEP 3129 also extended this syntax to be able to be used by user defined classes. 

Properties

Properties are a special case of the use of decorators. In the case of properties, the property function gets called on the function that is either a attribute setter, getter, deleter method of a class. The following two examples are equivalent (taken from https://wiki.python.org/moin/PythonDecoratorLibrary)

class C(object):
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x
    def setx(self, value):
        self._x = value
    def delx(self):
        del self._x
    x = property(getx, setx, delx, "I'm the 'x' property.")

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

Examples of decorators

There is a repository containing numerous examples at https://wiki.python.org/moin/PythonDecoratorLibrary.

6/03/2014

How to network between to VirtualBox virtual machines

How to network between to VirtualBox virtual machines


The problem is that the default NAT networking assigns every machine 10.0.2.15.
NAT works well for Internet access, but not to connect to other virtual machines.

Change the Network adapter to Bridged and then select an active adapter from the host on both guests.
Configure each guest with a unique address that is on the bridged network.
Bridged will allow for virtual machines to connect to each other, but not necessarily to the Internet.

Configure two interfaces: one NAT and one bridged for the best of both worlds!

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.