我是python decorators的新手.我借助简单的例子理解了基本概念.但是当我试图阅读这个更实用的装饰时,我感到迷茫.以下是我的问题所遵循的代码:
class countcalls(object):
"Decorator that keeps track of the number of times a function is called."
__instances = {}
def __init__(self, f):
self.__f = f
self.__numcalls = 0
countcalls.__instances[f] = self
def __call__(self, *args, **kwargs):
self.__numcalls += 1
return self.__f(*args, **kwargs)
def count(self):
"Return the number of times the function f was called."
return countcalls.__instances[self.__f].__numcalls
@countcalls
def f():
print 'f called'
f()
f()
f()
print f.count() # prints 3
Run Code Online (Sandbox Code Playgroud)
我的疑惑:
当我们将装饰器添加到函数前面时,这是否意味着我们正在创建装饰器类的对象?在我们的例子中,当它说:
@countcalls
def f():
print 'f called'
是@countcalls …
我试图了解如何使用这两种方法.我知道这__new__ 用于创建一个对象,同时__init__进行初始化.但是我不确定在创建对象时会发生什么.
这是否意味着__new__和__init__必须具有相同的参数?
如果我不使用相同的参数会发生什么?
可能重复:
*args和**kwargs是什么意思?
从阅读这个例子和我对Python的渺小知识来看,它必须是将数组转换为字典的快捷方式吗?
class hello:
def GET(self, name):
return render.hello(name=name)
# Another way:
#return render.hello(**locals())
Run Code Online (Sandbox Code Playgroud) 我理解除第一个以外的所有功能.什么(*args)是什么意思?
谢谢
def print_twice(*args):
arg1, arg2 = args
print 'arg1: %r arg2: %r' % (arg1, arg2)
def print_twice_again(arg1, arg2):
print 'arg1: %r arg2: %r' % (arg1, arg2)
def print_once(arg1):
print 'arg1: %r' % arg1
def print_none():
print 'i got nothin...'
print_twice("neil", "harper")
print_twice_again("neil", "harper")
print_once("first!")
print_none()
Run Code Online (Sandbox Code Playgroud) 可能重复:
*args和**kwargs是什么意思?
我正在阅读挖掘社交网络并遇到一个我无法弄清楚的python语法:
transforms = [(', Inc.', ''), (', Inc', ''), (', LLC', ''), (', LLP', '')]
"google, Inc.".replace(*transforms[0])
Run Code Online (Sandbox Code Playgroud)
但如果我输入
*transforms[0]
Run Code Online (Sandbox Code Playgroud)
在解释器中,它说它是无效的语法.我用Google搜索了,但是python文档真的不适合这份工作.
那么星号在这里意味着什么呢?谢谢你们.
我有一个字符串列表,我想在我的django自定义命令中传递给args.
list = ['abc', 'def', 'ghi', etc...]
Run Code Online (Sandbox Code Playgroud)
我怎样才能在python函数中执行此操作:
management.call_command('commandname', args, options)
Run Code Online (Sandbox Code Playgroud)
我已经厌倦了传递我的args列表:
[1]直接:
management.call_command('commandname', list)
Run Code Online (Sandbox Code Playgroud)
和
[2]作为循环:
management.call_command('commandname', (abc for abc in list))
Run Code Online (Sandbox Code Playgroud)
但是在输入自定义命令后两者都失败了
有效负载是逗号分隔的数据块,代表来自逆变器的读数。这些读数需要存储在名为InverterHistory的表中。每个字段都不会总是有一个值。该模型已经设置为允许字段为空/空白。因此,我试图在将其分配给字段之前检查是否存在一个值。这是我到目前为止的内容:
i = Inverter.objects.get(mac=u)
payload.reverse()
try:
ac_volts_a = str(payload.pop())
ac_volts_b = str(payload.pop())
ac_volts_c = str(payload.pop())
ac_current_a = str(payload.pop())
ac_current_b = str(payload.pop())
ac_current_c = str(payload.pop())
dc_volts = str(payload.pop())
dc_current = str(payload.pop())
kw_out = str(payload.pop())
mwh_total = str(payload.pop())
current_time = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
i_data = InverterHistory(
inverter = i,
if ac_volts_a:
voltage_ac_a = float(ac_volts_a),
if ac_volts_b:
voltage_ac_b = float(ac_volts_b),
if ac_volts_c:
voltage_ac_c = float(ac_volts_c),
if ac_current_a:
current_ac_a = float(ac_current_a),
if ac_current_b:
current_ac_b = float(ac_current_b),
if ac_current_c:
current_ac_c = float(ac_current_c),
if dc_volts:
voltage_dc = …Run Code Online (Sandbox Code Playgroud) 所以在java中你可以做这样的事情,如果你不知道你将获得多少参数
private void testMethod(String... testStringArray){
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在python中做这样的事情
因为我不能做这样的事情吧?
def testMethod(...listA):
Run Code Online (Sandbox Code Playgroud) 如果我有这个功能:
def foo(arg_one, arg_two):
pass
Run Code Online (Sandbox Code Playgroud)
我可以像这样包装它:
def bar(arg_one, arg_two):
return foo(arg_one, arg_two)
foo = bar
Run Code Online (Sandbox Code Playgroud)
是否有可能在不知道foo必需参数的情况下这样做,如果是这样,怎么做?
我想将list的元素作为函数参数传递,如下所示:
def foo(a,b,c):
#do something
list=[1,2,3]
foo(list)
Run Code Online (Sandbox Code Playgroud)
我不能使用foo(list [0],list [1],list [2]),因为我不知道有多少元素列表有多少参数函数需要
看看这段代码中的输入函数:http://pastebin.com/ajDrFmzr
现在它只传递1个参数(列表),我想把它作为少数参数传递