我在删除临时表时遇到问题.用户帐户没有"drop"权限.出于安全原因,我不想授予该权限.我试图找到像'临时'一样的特权,但没有.似乎唯一的选择是删除所有'drop table'语句.我知道在数据库会话结束后,临时表将自动删除.但是,我不确定是否有任何副作用将此作业留给MySQL.请指教.
我遇到了方法覆盖的问题.
看下面的src代码,
class Foo(object):
@staticmethod
def bar():
pass
Foo.bar() # works fine
print Foo.bar # <function bar at 0x028A5B30>
print dir(Foo.bar)
"""
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__',
'__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__',
'__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
'func_doc', 'func_globals', 'func_name']
"""
backup = Foo.bar # keep the instance of the method object
Foo.bar = backup # overwrite with the same method object
print Foo.bar # <unbound method …Run Code Online (Sandbox Code Playgroud) 我正在用Python编写测试自动化工具.该工具的一个关键特性是通过具有各种签名的名称来调用方法,就像C#反射一样.然而,在阅读了一堆文章并做了几次测试之后,我找不到处理各种签名的方法.
这是我的第一个想法 -
def invoke(obj, method_name, *args):
print type(args)
method = getattr(obj, method_name)
method(*args)
import sys
module = sys.modules[__name__]
invoke(module, 'foo', 1, 2)
Run Code Online (Sandbox Code Playgroud)
它确实有效.但问题是,名称调用的方法可以有不同数量的参数.然后我认为参数列表可以由元组重新表示,因为args的类型是一个元组.所以我改变了最后一行代码 -
invoke(module, 'foo', (1, 2)) # pass parameter list using a tuple (1, 2)
Run Code Online (Sandbox Code Playgroud)
但翻译告诉我这个 -
Traceback (most recent call last):
File "\Src\studies\dynamic_method_call.py", line 14, in <module>
invoke(module, 'foo', (1, 2))
File "\Src\studies\dynamic_method_call.py", line 9, in invoke
print method(*args)
TypeError: foo() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
我也尝试过list和keywored args.他们都没有工作.请指教!