使用元类实例化类,使用类函数分配属性
我想使用类定义内部的函数在类实例化时定义属性。
派生取决于一个变化变量 (self.name) 或对象的名称,该名称在实例化期间传递。
class Dog:
def __init__(self,
name,
date,
oranges_in_stomach
):
self.name = name
self.date = date
self.oranges_in_stomach = oranges_in_stomach
def inspect_stomach(self):
x=self.name
return x
def count_oranges_in_stomach(self):
n_oranges = inspect_stomach(self)
return n_oranges
Husky = Dog(
'Husky'
'00:00:0000',
oranges_in_stomach = self.oranges_in_stomach(self)
)
显然:
NameError: name 'self' is not defined
如何让 python 首先执行函数 count_oranges_in_stomach(),从 **Husky** = Dog('husky'...)
中获取 self.name
或Husky.name
然后将结果分配给 Husky.oranges_in_stomach
?
我尝试过使用 globals()
但到目前为止没有效果。 对元类主题完全陌生 - 如果我理解正确,我必须创建一个像这样的元类:
class Meta(type):
def __new__(cls, name, bases, dct):
x = super().__new__(cls, name, bases, dct)
x.name = name
return x
如何指定 x.name 等于我将在此处键入的内容:Husky =
我上面的例子已经完成了吗?它抛出一个TypeError: type.__new__(X): X is not a type object (str)
。 [为什么错误中“X”是大写字母?]
如果我设法将橙色计数函数放入元类中,如何使用名称调用该函数? [我如何访问基础,dct 进行更多实验?]
换句话说 - 我希望元类调用此:
Husky.count_oranges_in_stomach()
在 Husky 实例化期间,一旦读取 Husky
并在完成之前,通过为 Husky.oranges_in_stomach
所以它不会导致错误。 在第二种方法中,在读取我的 self.name 定义后立即调用它。
如果我理解正确的话,元类是实例化的矩阵。如何利用它们来实现这些目标?
感谢提示:)
I want to define attribute at class instantiation using a function from inside the class definition.
The derivation depends on one varying variable (self.name) or the very name of the object, which is passed on during instantiation.
class Dog:
def __init__(self,
name,
date,
oranges_in_stomach
):
self.name = name
self.date = date
self.oranges_in_stomach = oranges_in_stomach
def inspect_stomach(self):
x=self.name
return x
def count_oranges_in_stomach(self):
n_oranges = inspect_stomach(self)
return n_oranges
Husky = Dog(
'Husky'
'00:00:0000',
oranges_in_stomach = self.oranges_in_stomach(self)
)
Obviously:
NameError: name 'self' is not defined
How to make python first execute the function count_oranges_in_stomach(), taking the self.name
from **Husky** = Dog('husky'...)
or Husky.name
and then assign the result to Husky.oranges_in_stomach
?
I've tried experimenting with globals()
but to no avail so far.
Completely new to the topic of metaclasses - and If I understand correctly, I'd have to create a metaclass like this:
class Meta(type):
def __new__(cls, name, bases, dct):
x = super().__new__(cls, name, bases, dct)
x.name = name
return x
How to specify that x.name would equal what I will type here: Husky =
Is it already done in my example above? It throws a TypeError: type.__new__(X): X is not a type object (str)
. [Why is "X" a capital letter in the error?]
If I managed to put that orange-counting function in the metaclass, how to call this function using the name? [How can I access the bases,dct to experiment more?]
In other words - I want the metaclass to call this:
Husky.count_oranges_in_stomach()
during the Husky instantiation, as soon as it reads Husky
and before it's done, pass that as Husky.oranges_in_stomach
so it doesn't cause error.
In the second method, call this as soon as it reads my self.name definition.
If I understand correctly, metaclasses are matrixes for instantiation. How to use them to accomplish those goals?
Hints appreciated:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你打算做的事情令人困惑,无法理解,而且没有办法回答这个问题。显然,代码不能发挥魔法:在示例代码中,您想要在实例化之前运行的方法(这对于类方法来说是可能的)取决于另一个读取属性的方法,该属性已设置在
__init__
代码中。很明显,如果该代码未运行,则不存在self.name
并且您的inspect_stomach
将失败。无法猜测如何“解决”这个问题,因为从一开始就没有意义。
说明您需要什么,并使用一些有意义的示例代码(将狗的名字返回为“橙子的数量”),也许有人能够帮助您。
感觉你所需要的只是类方法和一些属性,但没有办法确定。
What you intend to do is confusing beyond understanding and there is no way to answer this. Obviously the code can't do magic: in your example code the method you want to run prior to instantiation (which is a possible thing with classmethods) depends on another method which reads an attribute, which is set inside the
__init__
code. It is clear that if that code is not run, there is noself.name
and yourinspect_stomach
will fail.There is no way to guess how to "fix" this, since it makes no sense to start with.
State what you need, with some example code that makes sense (out of returning a dog's name as "number of oranges"), and maybe someone will be able to help you.
It feels like all you need are classmethods and some properties, but there is no way to tell for sure.