8

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?

6 Answers 6

7
>>> 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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Santa Atom's __init__ should pass any extra arguments it gets that it doesn't need up to its superclass. Adding the id_ parameter to Atom's __init__ violates the DRY principle. Imagine adding more classes that inherit from Node as well, using the id_ parameter and passing it up, and a Molecule class that inherits from Atom (even though it technically fails the "is a" test). Now you want to add a location parameter to the Node __init__ and you have to change it in the __init__ of Node, the __init__ of all its subclasses, the __init__ of all the subclasses of its subclasses, etc
@user470379 Like I said, it's a proof of concept. There is no need to over-engineer it.
6

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

Comments

3

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
>>>

Comments

2

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

Comments

1
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.

Comments

1
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.

1 Comment

It's good practice to invoke the base class __init__ method before doing any other initialization, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.