Can't I define a decorator in a separate file and import it?

Saqib Ali saqib.ali.75 at gmail.com
Thu Dec 22 16:09:20 EST 2011


BTW Here is the traceback:

>>> import myClass
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "myClass.py", line 6, in <module>
    @Singleton
TypeError: 'module' object is not callable



Here is Singleton.py:



class Singleton:

    def __init__(self, decorated):
        self._decorated = decorated

    def Instance(self):
        try:
            return self._instance
        except AttributeError:
            self._instance = self._decorated()
            return self._instance

    def __call__(self):
        raise TypeError(
            'Singletons must be accessed through the `Instance`
method.')



Here is myClass.py:

#!/usr/bin/env python
import os, sys, string, time, re, subprocess
import Singleton


@Singleton
class myClass:

    def __init__(self):
        print 'Constructing myClass'

    def __del__(self):
        print 'Destructing myClass'



More information about the Python-list mailing list