cuốn sách gpt4 ai đã làm

Python:如何取消隐藏关键字 'property' ?

In lại Tác giả: Vũ trụ không gian 更新时间:2023-11-03 13:05:21 26 4
mua khóa gpt4 Nike

我支持一个Phiên bản cũ python 应用程序,它有一个这样编写的类(仍在 python 2.4 中运行):

class MyClass(object):

def property(self, property_code, default):
...

现在我要向它添加一些新代码:

    def _check_ok(self):
...

ok = property(lamdba self:self._check_ok())

基本上我想给这个类添加一个属性'ok'.

但是它不起作用。我遇到了这个错误信息:

TypeError: property() takes at least 2 arguments (1 given)

现有的类方法“property”已经盖过了内置的“property”关键字。

有什么方法可以让我在新代码中使用 'property' 吗?

重构现有的 property() 函数不是一个选项。

biên tập: 如果我将新代码放在 MyClass::property def 之前,它将起作用。不过很想看看有没有更好的解决办法

编辑 2:这些代码在 shell 中工作

>>> class Jack(object):
... def property(self, a, b, c):
... return 2
... p = __builtins__.property(lambda self: 1)
...
>>> a = Jack()
>>> a.p
1
>>> a.property(1, 2, 3)
2

但同样的技术在我的应用程序中不起作用。得到 AttributeError: 'dict' object has no attribute 'property' sai lầm

câu trả lời hay nhất

python 2:

import __builtin__
__builtin__.property

python 3:

import builtins
builtins.property

关于Python:如何取消隐藏关键字 'property' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621718/

26 4 0
không gian vũ trụ
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress