我已经阅读了python文档中的示例,但仍然无法弄清楚这个方法的含义.有人可以帮忙吗?以下是python文档中的两个示例
>>> from collections import defaultdict
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
... d[k] += 1
...
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
Run Code Online (Sandbox Code Playgroud)
和
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
Run Code Online (Sandbox Code Playgroud)
参数int和list用于什么?
我知道__call__在调用类的实例时会触发类中的方法.但是,我不知道何时可以使用这种特殊方法,因为可以简单地创建一个新方法并执行在__call__方法中完成的相同操作,而不是调用实例,您可以调用该方法.
如果有人给我这种特殊方法的实际用法,我将非常感激.
Python函数可以作为另一个函数的参数吗?
说:
def myfunc(anotherfunc, extraArgs):
# run anotherfunc and also pass the values from extraArgs to it
pass
Run Code Online (Sandbox Code Playgroud)
所以这基本上是两个问题:
BTW,extraArgs是anotherfunc参数的列表/元组.
假设我在Python单元测试中有以下代码:
aw = aps.Request("nv1")
aw2 = aps.Request("nv2", aw)
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以断言aw.Clear()在测试的第二行中调用了一个特定的方法(在我的例子中)?例如,有这样的事情:
#pseudocode:
assertMethodIsCalled(aw.Clear, lambda: aps.Request("nv2", aw))
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行此代码,我有一个列表列表.我需要添加到内部列表,但我得到错误
TypeError: 'list' object is not callable.
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我这里我做错了什么.
def createlists():
global maxchar
global minchar
global worddict
global wordlists
for i in range(minchar, maxchar + 1):
wordlists.insert(i, list())
#add data to list now
for words in worddict.keys():
print words
print wordlists(len(words)) # <--- Error here.
(wordlists(len(words))).append(words) # <-- Error here too
print "adding word " + words + " at " + str(wordlists(len(words)))
print wordlists(5)
Run Code Online (Sandbox Code Playgroud) 让我说我的类有很多方法,我想在每一个方法上应用我的装饰,后来当我添加新方法时,我想要应用相同的装饰器,但我不想在方法声明之上写@mydecorator所有时间?
如果我调查的__call__是正确的方法吗?
重要提示:以下示例似乎解决了与原始问题不同的问题.
编辑:我想以这种方式展示,这是我的问题的类似解决方案,对于任何人后来发现这个问题,使用评论中提到的mixin.
class WrapinMixin(object):
def __call__(self, hey, you, *args):
print 'entering', hey, you, repr(args)
try:
ret = getattr(self, hey)(you, *args)
return ret
except:
ret = str(e)
raise
finally:
print 'leaving', hey, repr(ret)
Run Code Online (Sandbox Code Playgroud)
然后你可以在另一个
class Wrapmymethodsaround(WrapinMixin):
def __call__:
return super(Wrapmymethodsaround, self).__call__(hey, you, *args)
Run Code Online (Sandbox Code Playgroud) 例如,我尝试了类似的东西mydict = {'funcList1': [foo(),bar(),goo()], 'funcList2': [foo(),goo(),bar()],但是没有用.
有这种功能的某种结构吗?
我意识到我可以用一堆def语句轻松地做到这一点:
def func1():
foo()
bar()
goo()
Run Code Online (Sandbox Code Playgroud)
但是我需要的陈述数量变得非常笨拙且难以记住.将它们很好地包装在字典中会很好,我可以一次又一次地检查键.
我是Python的新手并且遵循教程.list教程中有一个例子:
example = list('easyhoss')
Run Code Online (Sandbox Code Playgroud)
现在,在教程中,example= ['e','a',...,'s'].但在我的情况下,我得到以下错误:
>>> example = list('easyhoss')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
请告诉我我错在哪里.我搜索了这个,但它是不同的.
作为Python的初级开发人员,我已经多次出现在我的控制台中看到此错误消息,但我不完全明白这意味着什么.
有人能用一般的方式告诉我,这种错误会产生什么样的行为?
python ×10
dictionary ×2
arguments ×1
call ×1
callable ×1
decorator ×1
defaultdict ×1
dispatch ×1
function ×1
list ×1
metaclass ×1
methods ×1
oop ×1
unit-testing ×1
wrapper ×1