为了理解*args和**kwargs我做了一些搜索,当我遇到这个问题时*args和**kwargs?
所选答案下面的答案引起了我的注意,这是:
class Foo(object):
def __init__(self, value1, value2):
# do something with the values
print value1, value2
class MyFoo(Foo):
def __init__(self, *args, **kwargs):
# do something else, don't care about the args
print 'myfoo'
super(MyFoo, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
我在这个例子上尝试了一些东西并以这种方式运行代码:
class Foo(object):
def __init__(self, value1, value2):
# do something with the values
print 'I think something is being called here'
print value1, value2
class MyFoo(Foo):
def __init__(self, *args, **kwargs):
# do something else, don't care about the args
print args, kwargs …Run Code Online (Sandbox Code Playgroud) 例如:
import mock
class MyClass(object):
def foo(self, x, y, z):
return (x, y, z)
class TestMyClass(TestCase)
@mock.patch('MyClass')
def TestMyClass(self, MyClassMock):
foo_mock = MyClassMock.foo()
self.assertEquals((x, y, z), foo_mock)
Run Code Online (Sandbox Code Playgroud)
所以,真正的问题是:如何获得返回的测试内容得到这个<MagicMock name='MyClass.foo()' id='191728464'>或如何处理这个MagicMock对象来获得该测试的返回,该测试应该是一个包含3个元素的元组,而不是更多或更少?
任何建议,任何想法,任何论点都将受到欢迎.提前致谢!
为什么当我尝试这样做时:
#include <stdio.h>
int main()
{
printf("Size of int: %d bytes\n",sizeof(int));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我懂了:
warning: format "%d" expects argmuments of int type, but the second argument is of type "long unsigned int..."
Run Code Online (Sandbox Code Playgroud)
(?)
如果它重要,我的操作系统是64位12.4版的Ubuntu.编译器是:GNU GCC编译器,IDE是Code :: Blocks.
为了好奇,我在另一台机器运行相同的代码,运行一个可怜的Win7,结构也是64位,我得到的结果是int的大小,而不是像上面的警告.
在python 2.7下.
如何处理那些只传递一个论点的方法,即"自我"?
class MyTwitterClass(object):
...
def keys(self):
return {
'consumer_key': self.read_settings().get('Keys', 'consumer_key'),
"consumer_secret": self.read_settings().get('Keys', 'consumer_secret'),
"access_token_key": self.read_settings().get('Keys', 'access_token_key'),
"access_token_secret": self.read_settings().get('Keys', 'access_token_secret')
}
api = twitter.Api(consumer_key=keys()['consumer_key'],
consumer_secret=keys()['consumer_secret'],
access_token_key=keys()['access_token_key'],
access_token_secret=keys()['access_token_secret'])
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Traceback (most recent call):
File "..." ...
...
api = twitter.Api(consumer_key=keys()['consumer_key'],
TypeError: keys() takes exactly 1 argument (0 given)
Run Code Online (Sandbox Code Playgroud)
任何想法都会受到欢迎.