本文介绍了python内置方法在任何地方的替代名称空间中都可用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可供参考的python 方法

Are the python built-in methods available to reference in a package somewhere?

让我解释一下。在python的早期,我制作了一个类似于django的模型:

Let me explain. In my early(ier) days of python I made a django model similar to this:

class MyModel(models.Model):
    first_name = models.CharField(max_length=100, null=True, blank=True)
    last_name = models.CharField(max_length=100, null=True, blank=True)
    property = models.ForeignKey("Property")

此后,我需要为其添加属性。这让我有了这个模型:

I have since needed to add a property to it. This leaves me with this model:

class MyModel(models.Model):
    first_name = models.CharField(max_length=100, null=True, blank=True)
    last_name = models.CharField(max_length=100, null=True, blank=True)
    property = models.ForeignKey("Property")

    @property
    def name(self):
        return "{} {}".format(first_name, last_name)

所以现在在运行时出现错误: TypeError:'ForeignKey'对象不可调用。发生这种情况的原因是,ForeignKey属性已替换了内置标识符属性。我想做的是,使用 @ sys.property (或类似的东西)代替 @property )。

So now at runtime I get the error: TypeError: 'ForeignKey' object is not callable. This is happening because the ForeignKey for property has replaced the built-in identifier property. What I would like to be able to do is, instead of @property use @sys.property (or something similar).

注意:我已经知道将name属性移到property字段声明上方的解决方法。我不太担心这种特殊情况,因为我是引用python内置文件的替代位置的主要问题。

Note: I already know about the workaround of moving the name property above the declaration of the property field. I am not so concerned about this particular case as I am the main question of alternative locations for referencing the python built-ins.

推荐答案

使用或如果您使用的是Python 2。

Use builtins, or __builtin__ if you're on Python 2.

def open():
    pass

import __builtin__

print open
print __builtin__.open

这给您:

<function open at 0x011E8670>
<built-in function open>

这篇关于python内置方法在任何地方的替代名称空间中都可用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:41