python 内置方法是否可以在任何地方的替代命名空间中使用?
python 内置方法是否可在包中引用某处?
让我解释一下。在我早期使用 python 的时候,我制作了一个与此类似的 django 模型:
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")
此后我需要向它添加一个属性。这给我留下了这个模型:
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' object is not callable
。发生这种情况是因为属性的ForeignKey 已替换了内置标识符属性。我希望能够做的是,使用 @sys.property
(或类似的东西)而不是 @property
。
注意:我已经知道将 name 属性移动到属性字段声明上方的解决方法。我不太关心这个特殊情况,因为我主要问题是引用 python 内置函数的替代位置。
Are the python built-in methods available to reference in a package somewhere?
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)
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).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
内置
,或__builtin__
如果您使用的是 Python 2。这将为您提供:
Use
builtins
, or__builtin__
if you're on Python 2.This gives you:
Python 有一个
builtins
模块,其中“真正的全局” “ 事物——通常只是存储标准内置函数和类型。在 Python 2 中,它被命名为 __builtin__ ,但工作原理基本相同。该模块可以像任何其他模块一样导入,但它也神奇地为每个其他模块提供内置名称(不会隐藏它们)。
如果您想知道它是如何工作的,
builtins
文档说:并且
exec
说:因此,至少在 CPython 中,当您计算
abs
时,它会在globals()['abs']
中查找,但在那里找不到,然后在>globals()['__builtins__'].__dict__['abs']
。每当 Python(或至少 CPython)创建一个新的模块对象时,它的代码都会针对带有
empty
__builtins__
的globals
执行,这意味着默认的builtins
模块值被填充,这样就可以工作了。这个globals
是为模块中定义的每个函数和类复制的(以及您使用globals
显式执行的任何操作,而不显式替换__builtins__
>),所以它也可以在函数和类中工作。Python has a
builtins
module where "truly global" things—normally just the standard builtin functions and types—are stored. In Python 2, it was named__builtin__
, but worked mostly the same.This module can be imported just like any other module—but it also magically supplies the builtin names for every other module (that doesn't hide them).
If you're wondering how that works, the
builtins
docs say:And
exec
says:So, at least in CPython, when you evaluate
abs
, it's looked up inglobals()['abs']
, not found there, and then looked up inglobals()['__builtins__'].__dict__['abs']
.And whenever Python (or at least CPython) creates a new module object, its code is executed against a
globals
with anempty
__builtins__
, which means the defaultbuiltins
module value gets filled in, so that works. And thisglobals
is the one that gets copied for very function and class defined in the module (and anything you do explicitly withglobals
without explicitly replacing__builtins__
), so it works inside functions and classes as well.