【问题标题】:IronPython 2.7.4: SystemError while initializing a propertyIronPython 2.7.4:初始化属性时出现 SystemError
【发布时间】:2013-12-11 16:51:33
【问题描述】:

我遇到了一个问题,我已经绞尽脑汁想了几个小时,但仍然无法理解为什么会发生,所以我发现自己转向 StackOverflow 寻求帮助......

我正在开发一个使用 IronPython 2.7.4 执行的相当大的应用程序(由多个脚本文件组成)。

我已经能够将代码简化为导致问题的部分。

原始脚本以与下面列出的相同方式定义了几个属性。
然而,这个,只有这个,会导致错误。

尝试导入脚本文件时以及将其作为入口点运行时都会发生这种情况。

这是简化的test.py 文件:

1 class Test(object):
2    def __init__(self):
3        self._P = 'Hello'
4
5    def P():
6        def fget(self):
7            return self._P
8    P = property(**P())        # ERROR

这是我尝试运行时打印出来的控制台:

$ ipy64 test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
File "test.py", line 8, in Test
SystemError: Object reference not set to an instance of an object.

(我也试过用ipy代替ipy64;没有区别。

如果有人能指出我错过了什么,我将不胜感激! :)

我正在运行 Windows 7 64 位家庭高级版。
(我在cmd中使用prompt $$在命令行中创建了$,因为目录路径很长!)

【问题讨论】:

  • 您希望P = property(**P()) 做什么? P 不返回任何东西,所以 P() 是 None,你不能 **-unpack None。 CPython 错误信息更有启发性:TypeError: type object argument after ** must be a mapping, not NoneType.

标签: python properties ironpython


【解决方案1】:

你在函数 P 中错过了self

def P(self):

【讨论】:

  • ... 等等。
【解决方案2】:

看来我是傻了……

Sublime Text 中,输入property 并点击Tab,自动完成到:

def foo():
    def fget(self):
        return self._foo
    def fset(self, value):
        self._foo = value
    def fdel(self):
        del self._foo
    return locals()
foo = property(**foo())

我的问题是,在删除属性的设置器和删除器时(因为我不需要它们)
我不小心删除了 return locals() 语句导致它不起作用。

是的...少说,更好! :))

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 1970-01-01
    • 2012-12-15
    • 2020-09-18
    • 2023-03-23
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多