我在sshfs和git存储库上使用emacs.我喜欢使用git命令行,所以对于这个项目,我不需要启用vc-git.如何通过.emacs命令阻止加载vc-git ?
在Python中,我希望能够创建一个既可以作为类函数又可以作为实例方法的函数,但能够更改行为.用例是针对一组可序列化的对象和类型.举个例子:
>>> class Thing(object):
#...
>>> Thing.to_json()
'A'
>>> Thing().to_json()
'B'
Run Code Online (Sandbox Code Playgroud)
我知道,鉴于Python源代码中funcob.c中classmethod()的定义,看起来它对于C模块来说很简单.有没有办法在python中执行此操作?
谢谢!
通过描述符的提示,我能够使用以下代码完成:
class combomethod(object):
def __init__(self, method):
self.method = method
def __get__(self, obj=None, objtype=None):
@functools.wraps(self.method)
def _wrapper(*args, **kwargs):
if obj is not None:
return self.method(obj, *args, **kwargs)
else:
return self.method(objtype, *args, **kwargs)
return _wrapper
Run Code Online (Sandbox Code Playgroud)
谢谢亚历克斯!
我记得曾经读过很好的写作集合指南.我的意思是,它描述了使用宏来生成带有类型参数的类型,类似于C++模板.我不确定它是不是由Rusty Russell写的,但它是我认出的人.它发布在hackernews或proggit上...我想写一个新的C库,并在过去的30分钟搜索谷歌这个指南无济于事.有人记得吗?
我正在开发一个库,它实现了一个适用于任何有序数据类型的数据结构 - 一个范围集.当你允许正负无限时,许多操作(如反转)会变得有趣.
一个目标是让datetime对象与该模块一起使用,并且在使用非数字对象支持infinity时,我创建了INFINITY和NEGATIVE_INFINITY:
class _Indeterminate(object):
def __eq__(self, other):
return other is self
@functools.total_ordering
class _Infinity(_Indeterminate):
def __lt__(self, other):
return False
def __gt__(self, other):
return True
def __str__(self):
return 'inf'
__repr__ = __str__
@functools.total_ordering
class _NegativeInfinity(_Indeterminate):
def __lt__(self, other):
return True
def __gt__(self, other):
return False
def __str__(self):
return '-inf'
INFINITY = _Infinity()
NEGATIVE_INFINITY = _NegativeInfinity()
Run Code Online (Sandbox Code Playgroud)
不幸的是,当在cmp()操作的左侧时,这对datetime对象不起作用:
In [1]: from rangeset import *
In [2]: from datetime import datetime
In [3]: now = datetime.now()
In [4]: cmp(INFINITY, now)
Out[4]: 1 …Run Code Online (Sandbox Code Playgroud)