constructor

How does multiple inheritance work with the super() and different __init__() arguments?

How does multiple inheritance work with the super() and different __init__() arguments? Question: I’m just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) …

Total answers: 2

How to override constructor of Python class with many arguments?

How to override constructor of Python class with many arguments? Question: Say, I have a class Foo, extending class Bar. And I want to slightly override Foo’s consructor. And I don’t want even know what signarure of Bar’s constructors is. Is there a way to do this? If you didn’t understand, I mean the following: …

Total answers: 2

How to force deletion of a python object?

How to force deletion of a python object? Question: I am curious about the details of __del__ in python, when and why it should be used and what it shouldn’t be used for. I’ve learned the hard way that it is not really like what one would naively expected from a destructor, in that it …

Total answers: 4

__init__ as a constructor?

__init__ as a constructor? Question: Dive into Python – It would be tempting but incorrect to call this the constructor of the class. It’s tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it’s the first piece of code executed in a newly …

Total answers: 6

Python: Inherit the superclass __init__

Python: Inherit the superclass __init__ Question: I have a base class with a lot of __init__ arguments: class BaseClass(object): def __init__(self, a, b, c, d, e, f, …): self._a=a+b self._b=b if b else a … All the inheriting classes should run __init__ method of the base class. I can write a __init__() method in each …

Total answers: 7

Is there a way to instantiate a class without calling __init__?

Is there a way to instantiate a class without calling __init__? Question: Is there a way to circumvent the constructor __init__ of a class in python? Example: class A(object): def __init__(self): print “FAILURE” def Print(self): print “YEHAA” Now I would like to create an instance of A. It could look like this, however this syntax …

Total answers: 6

Python constructor and default value

Python constructor and default value Question: Somehow, in the Node class below, the wordList and adjacencyList variable is shared between all instances of Node. >>> class Node: … def __init__(self, wordList = [], adjacencyList = []): … self.wordList = wordList … self.adjacencyList = adjacencyList … >>> a = Node() >>> b = Node() >>> a.wordList.append(“hahaha”) …

Total answers: 4

Automatically setting class member variables in Python

Automatically setting class member variables in Python Question: Say, I have the following class in Python class Foo(object): a = None b = None c = None def __init__(self, a = None, b = None, c = None): self.a = a self.b = b self.c = c Is there any way to simplify this process? …

Total answers: 4

How to invoke the super constructor in Python?

How to invoke the super constructor in Python? Question: class A: def __init__(self): print(“world”) class B(A): def __init__(self): print(“hello”) B() # output: hello In all other languages I’ve worked with the super constructor is invoked implicitly. How does one invoke it in Python? I would expect super(self) but this doesn’t work. Asked By: Mike || …

Total answers: 7

How to create a class instance without calling initializer?

How to create a class instance without calling initializer? Question: Is there any way to avoid calling __init__ on a class while initializing it, such as from a class method? I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a …

Total answers: 4