小编Mik*_*iak的帖子

如何在emacs中禁用vc-git?

我在sshfs和git存储库上使用emacs.我喜欢使用git命令行,所以对于这个项目,我不需要启用vc-git.如何通过.emacs命令阻止加载vc-git ?

git emacs

26
推荐指数
3
解决办法
7997
查看次数

创建同时是实例和类方法的方法

在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)

谢谢亚历克斯!

python methods

17
推荐指数
1
解决办法
1205
查看次数

在哪里可以找到编写C集合的好指南?

我记得曾经读过很好的写作集合指南.我的意思是,它描述了使用宏来生成带有类型参数的类型,类似于C++模板.我不确定它是不是由Rusty Russell写的,但它是我认出的人.它发布在hackernews或proggit上...我想写一个新的C库,并在过去的30分钟搜索谷歌这个指南无济于事.有人记得吗?

c

7
推荐指数
1
解决办法
231
查看次数

在python中为任何对象创建无穷大和负无穷大

我正在开发一个库,它实现了一个适用于任何有序数据类型的数据结构 - 一个范围集.当你允许正负无限时,许多操作(如反转)会变得有趣.

一个目标是让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)

python comparison magic-methods

4
推荐指数
1
解决办法
906
查看次数

标签 统计

python ×2

c ×1

comparison ×1

emacs ×1

git ×1

magic-methods ×1

methods ×1