使用带有装饰器的docstrings时遇到问题.给出以下示例:
def decorator(f):
def _decorator():
print 'decorator active'
f()
return _decorator
@decorator
def foo():
'''the magic foo function'''
print 'this is function foo'
help(foo)
Run Code Online (Sandbox Code Playgroud)
现在,帮助没有向我显示foo预期的文档字符串,它显示:
Help on function _decorator in module __main__:
_decorator()
Run Code Online (Sandbox Code Playgroud)
没有装饰器,帮助是正确的:
Help on function foo in module __main__:
foo()
the magic foo function
Run Code Online (Sandbox Code Playgroud)
我知道,该函数foo由装饰器包装,因此函数对象不再是该函数foo.但是,按预期获得文档字符串(和帮助)的好方法是什么?
Python 3.6的一个功能是格式化字符串.
这个SO问题(python-3.6中带有'f'前缀的字符串)询问格式化字符串文字的内部,但我不理解格式化字符串文字的确切用例.我应该在哪些情况下使用此功能?不明确比隐含更好吗?
我在我的应用程序中使用asyncio,我对将事件循环作为参数传递感到困惑.
使用事件循环编写函数/方法时有三种可能性:
asyncio.get_event_loop()asyncio.get_event_loop()似乎最后一种情况大部分时间都在使用,但即使在asyncio api中,使用也是不一致的.因为我没有缩进使用两个分离的事件循环,所以只是asyncio.get_event_loop()在需要的地方使用?
什么是最好的方式?
单元测试模块时遇到了一个令人困惑的问题.该模块实际上是铸造值,我想比较这些值.
是有区别的使用比较==和is(部分,我小心的区别)
>>> 0.0 is 0.0
True # as expected
>>> float(0.0) is 0.0
True # as expected
Run Code Online (Sandbox Code Playgroud)
正如预期到现在,但这是我的"问题":
>>> float(0) is 0.0
False
>>> float(0) is float(0)
False
Run Code Online (Sandbox Code Playgroud)
为什么?至少最后一个对我来说真的很困惑.内部表示float(0)和float(0.0)应该是平等的.与之比较==正如预期的那样.
python floating-point python-2.7 python-3.x python-internals
我有一个小样本函数:
#define VALUE 0
int test(unsigned char x) {
if (x>=VALUE)
return 0;
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器警告我,比较(x> = VALUE)在所有情况下都是正确的,这是正确的,因为x是无符号字符,VALUE定义为值0.所以我将代码更改为:
if ( ((signed int) x ) >= ((signed int) VALUE ))
Run Code Online (Sandbox Code Playgroud)
但警告又来了.我测试了三个GCC版本(所有版本> 4.0,有时你必须启用-Wextra).
在更改的情况下,我有这个显式的强制转换,它应该是一个有符号的int比较.为什么声称这种比较总是正确的?
给出以下示例:
class Foo(object):
def __init__(self, value=0):
self.value=value
def __int__(self):
return self.value
Run Code Online (Sandbox Code Playgroud)
我想要一个类Foo,它充当整数(或浮点数).所以我想做以下事情:
f=Foo(3)
print int(f)+5 # is working
print f+5 # TypeError: unsupported operand type(s) for +: 'Foo' and 'int'
Run Code Online (Sandbox Code Playgroud)
第一个语句print int(f)+5正在工作,因为有两个整数.第二个是失败的,因为我必须实现__add__与我的类一起执行此操作.
因此要实现整数行为,我必须实现所有整数模拟方法.我怎么能绕过这个呢.我试图继承int,但这种尝试没有成功.
更新
继承int失败,如果你想使用__init__:
class Foo(int):
def __init__(self, some_argument=None, value=0):
self.value=value
# do some stuff
def __int__(self):
return int(self.value)
Run Code Online (Sandbox Code Playgroud)
如果你然后打电话:
f=Foo(some_argument=3)
Run Code Online (Sandbox Code Playgroud)
你得到:
TypeError: 'some_argument' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
使用Python 2.5和2.6进行测试
在我们公司中,我们使用的是Subversion。我们使用不同版本的不同python模块(自有和第三方)。我们开发的各种应用程序对共享模块的版本具有各种依赖性。
一种可能性是使用virtualenv从本地pypi服务器安装模块。因此,在每次初始结帐时,我们都需要创建一个virtualenv,将其激活并从requirements.txt安装相关模块。
缺点:
因此,我们提出了另一个解决方案,请征询您的意见:在应用程序的路径中,我们使用svn:externals(又名git子模块)将其“链接”到指定的模块(从其发布路径并保留指定的修订号) (只读),因此该模块将被本地放置在应用程序的路径中。“ import mylib”将在python站点软件包或virtualenv中安装时起作用。甚至可以将wx,numpy和其他常用库的发行版放到我们的存储库中并在本地链接它们。
优点是:
实际的问题是:是否使用此方案在github / sorceforge上有项目?为什么每个人都使用virtualenv代替这个(看似)更简单的方案?我从未见过这样的解决方案,所以也许我们错过了一点?
PS:我已经在pypa-dev邮件列表上发布了此内容,但对于此类问题似乎是错误的地方。请原谅这个交叉口。
是否可以生成namedtuple从基类继承的 a?
我想要的是Circleand Rectangleare namedtuples 并且是从一个公共基类 ('Shape') 继承的:
from collections import namedtuple
class Shape:
def addToScene(self, scene):
...
Circle=namedtuple('Circle', 'x y radius')
Rectangle=namedtuple('Rectangle', 'x1 y1 x2 y2')
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我可以写一个描述符,返回一个可以等待的未来。
class AsyncDescriptor:
def __get__(self, obj, cls=None):
# generate some async future here
return future
def __set__(self, obj, value):
# generate some async future here
return future
class Device:
attr=AsyncDescriptor()
device=Device()
Run Code Online (Sandbox Code Playgroud)
现在,我可以用来获得协程中的值value=await device.attr。
我将如何设置此属性?
await device.attr=5 -> SyntaxError:无法分配给等待表达式await setattr(device, 'attr', 5) -> TypeError:对象NoneType不能在'await'表达式中使用device.attr=5 -> RuntimeWarning:从未等待协程'__set__'python ×8
python-3.5 ×2
casting ×1
decorator ×1
descriptor ×1
docstring ×1
emulation ×1
f-string ×1
gcc ×1
gcc-warning ×1
git ×1
inheritance ×1
integer ×1
literals ×1
namedtuple ×1
packaging ×1
python-2.7 ×1
python-3.6 ×1
python-3.x ×1
svn ×1
virtualenv ×1