小编Ang*_*elo的帖子

Python装饰器跳过装饰函数的代码

此代码跳过number函数中的代码:

def some_decorator(x):
     def wrapper(x):
         return x+1
     return wrapper

@some_decorator
def number(x):
    x = x + 100
    return x
Run Code Online (Sandbox Code Playgroud)

输出:

>>> number(3)
4
Run Code Online (Sandbox Code Playgroud)

我试图使输出为number(3)104,代码有什么问题?

python decorator

2
推荐指数
1
解决办法
35
查看次数

如何比较带有浮点数或整数的类返回?

我想将__repr__类的浮点数或整数进行比较。

class TestClass:   
    def __init__(self, a = 5): 
        self.a = a     
    def __repr__(self):
        return self.a
Run Code Online (Sandbox Code Playgroud)

显然会返回错误,因为a不是string

TypeError: __repr__ returned non-string (type int)
Run Code Online (Sandbox Code Playgroud)

如果将其设置为字符串,则打印正确:

>>> TestClass()
5
Run Code Online (Sandbox Code Playgroud)

但打印结果不可比:

>>> TestClass() == 5
False
Run Code Online (Sandbox Code Playgroud)

我应该用什么来比较类返回使它TestClass() == 5True

python integer compare class

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

标签 统计

python ×2

class ×1

compare ×1

decorator ×1

integer ×1