理解 Python 中的对象

发布于 2024-10-01 07:58:05 字数 531 浏览 4 评论 0原文

我对 Python 的对象模型有点困惑。我有两个类,一个类继承另一个类。

class Node():
  def __init__(identifier):
    self.identifier = identifier

class Atom(Node):
  def __init__(symbol)
    self.symbol = symbol

我想做的不是重写 __init__() 方法,而是创建一个具有属性 symbol 的atom实例> 和标识符

像这样:

Atom("Fe", 1) # will create an atom with symbol "Fe" and identifier "1"

因此,我希望在创建 Atom 实例后能够访问 Atom.identifier 和 Atom.symbol。

我怎样才能做到这一点?

I am a little confused by the object model of Python. I have two classes, one inherits from the other.

class Node():
  def __init__(identifier):
    self.identifier = identifier

class Atom(Node):
  def __init__(symbol)
    self.symbol = symbol

What I am trying to do is not to override the __init__() method, but to create an instance of atom that will have attributes symbol and identifier.

Like this:

Atom("Fe", 1) # will create an atom with symbol "Fe" and identifier "1"

Thus I want to be able to access Atom.identifier and Atom.symbol once an instance of Atom is created.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏尔 2024-10-08 07:58:05
>>> class Node(object):
...     def __init__(self, id_):
...             self.id_ = id_
... 
>>> class Atom(Node):
...     def __init__(self, symbol, id_):
...             super(Atom, self).__init__(id_)
...             self.symbol = symbol
... 
>>> a = Atom("FE", 1)
>>> a.symbol
'FE'
>>> a.id_
1
>>> type(a)
<class '__main__.Atom'>
>>> 

在代码中从对象继承是个好主意。

>>> class Node(object):
...     def __init__(self, id_):
...             self.id_ = id_
... 
>>> class Atom(Node):
...     def __init__(self, symbol, id_):
...             super(Atom, self).__init__(id_)
...             self.symbol = symbol
... 
>>> a = Atom("FE", 1)
>>> a.symbol
'FE'
>>> a.id_
1
>>> type(a)
<class '__main__.Atom'>
>>> 

It's a good idea to inherit from object in your code.

向日葵 2024-10-08 07:58:05

您必须手动调用超类的 __init__ 方法。

class Atom(Node):
  def __init__(self, symbol, identifier)
    Node.__init__(self, identifier)
    self.symbol = symbol

You have to call the __init__-method of the super-class manually.

class Atom(Node):
  def __init__(self, symbol, identifier)
    Node.__init__(self, identifier)
    self.symbol = symbol
无风消散 2024-10-08 07:58:05

创建类时,您需要在声明中使用 self 词。之后您可以定义其他参数。要继承,请调用超级 init 方法:

>>> class Node():
...   def __init__(self, identifier):
...     self.identifier = identifier
...
>>>
>>> class Atom(Node):
...   def __init__(self, symbol, identifier):
...     Node.__init__(self, identifier)
...     self.symbol = symbol
...
>>>
>>>
>>> fe = Atom("Fe", 1)
>>> fe.symbol
'Fe'
>>> fe.identifier
1
>>>

When creating a class you need to use the self word in the declaration. After that you can define the other arguments. To inherit call the super init method:

>>> class Node():
...   def __init__(self, identifier):
...     self.identifier = identifier
...
>>>
>>> class Atom(Node):
...   def __init__(self, symbol, identifier):
...     Node.__init__(self, identifier)
...     self.symbol = symbol
...
>>>
>>>
>>> fe = Atom("Fe", 1)
>>> fe.symbol
'Fe'
>>> fe.identifier
1
>>>
你的他你的她 2024-10-08 07:58:05

您的代码中缺少两个内容:

  1. 属于类的方法必须具有显式的 self 参数,而您缺少该参数

  2. 您的派生“Atom”类还需要接受用于初始化基类的参数。

更像是:

class Node():
  def __init__(self, identifier):
    self.identifier = identifier

class Atom(Node):
  def __init__(self, identifier, symbol)
    Node.__init__(self, identifier)
    self.symbol = symbol

You have two missing things in your code:

  1. methods belonging to a class have to have an explicit self parameter, which you are missing

  2. Your derived 'Atom' class also needs to accept the parameter it needs to use to initialize the base class.

Something more like:

class Node():
  def __init__(self, identifier):
    self.identifier = identifier

class Atom(Node):
  def __init__(self, identifier, symbol)
    Node.__init__(self, identifier)
    self.symbol = symbol
時窥 2024-10-08 07:58:05
class Node(object): 
  def __init__(self, identifier): 
    self.identifier = identifier 

class Atom(Node): 
  def __init__(self, symbol, *args, **kwargs)
    super(Atom, self).__init__(*args, **kwargs)
    self.symbol = symbol

要点:

  • 节点应该继承自object
  • 使用super调用父类的__init__函数。
  • Python 中的类成员函数将 self 作为第一个参数。
class Node(object): 
  def __init__(self, identifier): 
    self.identifier = identifier 

class Atom(Node): 
  def __init__(self, symbol, *args, **kwargs)
    super(Atom, self).__init__(*args, **kwargs)
    self.symbol = symbol

Points:

  • Node should inherit from object.
  • Use super to call parent classes' __init__ functions.
  • Class member functions take self as the first parameter in Python.
祁梦 2024-10-08 07:58:05
class Node(): 
  def __init__(self, identifier): 
    self.identifier = identifier 

class Atom(Node): 
  def __init__(self, symbol, *args, **kwargs)
    super(Atom, self).__init__(*args, **kwargs)
    self.symbol = symbol

有关 *args< 的说明,请参阅此处 /code> 和 **kwargs。通过使用 super 可以访问基类Atom 类的(超类)并将其命名为 __init__。此外,还需要包含 self 参数。

class Node(): 
  def __init__(self, identifier): 
    self.identifier = identifier 

class Atom(Node): 
  def __init__(self, symbol, *args, **kwargs)
    super(Atom, self).__init__(*args, **kwargs)
    self.symbol = symbol

See here for an explanation of the *args and **kwargs. By using super, you can access the base class (superclass) of the Atom class and call it's __init__. Also, the self parameter needs to be included as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文