由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - python: how to import a decorator?
相关主题
判断Python程序员水平的一个试题大牛帮我看看这个test code为什么complie 不了啊
请问关于c++实现singleton的问题?sbt错误:object xxx is not a member of package yyy
c++ singleton questionsuse clojure data collection in java..
C++类的静态函数对成员函数问JavaScript大牛一个问题
C++里最常用的design pattern都有哪些?C++的exception大家常用吗?
工厂模式 (转载)使用assert应遵循什么原则?
python decorator 调用问题[合集] 为什么很少有人用static来实现signleton?
js: 怎么来监听某个function被call的次数?Visual C++ 高手帮忙,一个Link Error
相关话题的讨论汇总
话题: subkey话题: none话题: instance1话题: instance2话题: key
进入Programming版参与讨论
1 (共1页)
S*******s
发帖数: 13043
1
I got an error if I want to run test.py:
@singleton
NameError: name 'singleton' is not defined
if I combine the two files, it would be fine.
what is right way to import decorator?
#in module Singleton.py
def singleton(theClass):
""" decorator for a class to make a singleton out of it """
classInstances = {}
def getInstance(*args, **kwargs):
""" creating or just return the one and only class instance.
The singleton depends on the parameters used in __init__ """
key = (theClass, args, str(kwargs))
if key not in classInstances:
classInstances[key] = theClass(*args, **kwargs)
return classInstances[key]
return getInstance
#in module test.py
import Singleton
@singleton
class A:
""" test class """
def __init__(self, key=None, subkey=None):
self.key = key
self.subkey = subkey
def __repr__(self):
return "A(id=%d, %s,%s)" % (id(self), self.key, self.subkey)
def tests():
""" some basic tests """
testCases = [ (None, None), (10, 20), (30, None), (None, 30) ]
instances = set()
instance1 = None
instance2 = None
for key, subkey in testCases:
if key == None:
if subkey == None: instance1, instance2 = A(), A()
else: instance1, instance2 = A(subkey=subkey), A(
subkey=subkey)
else:
if subkey == None: instance1, instance2 = A(key), A(key)
else: instance1, instance2 = A(key, subkey=subkey),
A(key, subkey=subkey)
print("instance1: %-25s" % instance1, " instance2: %-25s" %
instance2)
assert instance1 == instance2
assert instance1.key == key and instance1.subkey == subkey
instances.add(instance1)
assert len(instances) == len(testCases)
if __name__ == '__main__':
tests()
f*******n
发帖数: 12623
2
A decorator is just a regular function that you use by putting @ in front of
it. So you just import it the same way you import any other variable
import Singleton
@Singleton.singleton
or
from Singleton import singleton
@singleton
S*******s
发帖数: 13043
3
Thanks. still confused, when we import sth, when do we need add the module
name as prefix when we need refer to the element in the imported module?
Also, can you explain a little on how the getinstance get called?

of

【在 f*******n 的大作中提到】
: A decorator is just a regular function that you use by putting @ in front of
: it. So you just import it the same way you import any other variable
: import Singleton
: @Singleton.singleton
: or
: from Singleton import singleton
: @singleton

f*******n
发帖数: 12623
4
If you import something using "import xxx", then just the module is imported
. You must add the module name as prefix to use the things inside.
import math
math.sqrt(4)
If you import something using "from xxx import ...", then those things you
list are imported directly into the namespace.
from math import sqrt
sqrt(4)
You can import everything from a module into the namespace like this:
from math import *
sqrt(4)
1 (共1页)
进入Programming版参与讨论
相关主题
Visual C++ 高手帮忙,一个Link ErrorC++里最常用的design pattern都有哪些?
我是否需要Move(C++11)?工厂模式 (转载)
请教一个C++的设计问题python decorator 调用问题
请教高手一个C++问题js: 怎么来监听某个function被call的次数?
判断Python程序员水平的一个试题大牛帮我看看这个test code为什么complie 不了啊
请问关于c++实现singleton的问题?sbt错误:object xxx is not a member of package yyy
c++ singleton questionsuse clojure data collection in java..
C++类的静态函数对成员函数问JavaScript大牛一个问题
相关话题的讨论汇总
话题: subkey话题: none话题: instance1话题: instance2话题: key