constructor

Why doesn't python support multiple constructors like Java?

Why doesn't python support multiple constructors like Java? Question: I know python isn’t Java—and I’m not trying to be biased towards Java or something. I barely even use Java. I know you can set a default value of a init parameter in python by doing something like: def __init__(name = "Quinn"): self.name = name but …

Total answers: 1

TypeError when calling parent class despite passing parameter

TypeError when calling parent class despite passing parameter Question: I have a class TextElement which inherits from PyQt-class QTextEdit and a custom class BaseElement. BaseElement needs the parameter "transmitter" which is passed by the parameter "transmitter" of TextElement itself. Despite doing everthing accordingly I get the TypeError: Traceback (most recent call last): File "…HotTeacherpackagetblock.py", line …

Total answers: 1

Python constructor "self = "

Python constructor "self = " Question: I was writing a class and at some point I decided that it would be nice to have a possibility to create an instance of the class using other instance of this same class. So basically the class would look like this: class Test: def __init__(self, a, b): if …

Total answers: 1

Python class multiple constructors

Python class multiple constructors Question: There is a class that I want to be constructed from a string in 2 different ways. Here is what I mean: class ParsedString(): def __init__(self, str): #parse string and init some fields def __init__2(self, str): #parse string in another way and init the same fields In Java I would …

Total answers: 1

Python updating list from constructor by object.attribute

Python updating list from constructor by object.attribute Question: If a class is created with the attributes: name and list and the name attribute has a default value and the list is appending the name. Is it possible in somehow when I create an object "a" and type "a.name = ‘x’ " that this ‘x’ will …

Total answers: 1

How do I add a value to a parameter in an init function in python

How do I add a value to a parameter in an init function in python Question: Would it work if I added these values in this way or do I need to add double quotes to the values like "ENGR", "ARTS", …etc"? Also am I supposed to use curly brackets or square brackets? def __init__(self, …

Total answers: 1

Why does pybind fail for functions without arguments?

Why does pybind fail for functions without arguments? Question: I have an overloaded constructor in C++ (default + other). My automatically generated pybind code looks like this: py::class_<MyClass>(m, "MyClass") .def( py::init<>(), py::kw_only() ) .def( py::init< std::valarray<std::string> >(), py::kw_only(), py::arg("my_var") ) When I delete the first constructor everything works fine. But for the first one I …

Total answers: 1

Defining __init__ using lambda

Defining __init__ using lambda Question: I know that we can define class functions like so : pp = lambda self,v: self.list[v] but I am struggling with the syntax for defining the constructor the same way : __init__ = lambda self: self.list = [] I googled around why this is not working but seems like nobody …

Total answers: 1

Workaround for Cython bindings library to C++ that lacks nullary constructor?

Workaround for Cython bindings library to C++ that lacks nullary constructor? Question: I’m working on a set of Cython bindings to expose a C++ compression library in python. The library uses the Pimpl pattern and, in particular, has no default empty constructors. I’m wrestling with using a class method that returns a CompressionConfig object, which …

Total answers: 2

Variables inside constructor without "self" – Python

Variables inside constructor without "self" – Python Question: I am trying to understand whether declaring variables inside constructors is an ok practice. I only need these variables inside the constructor. I am asking this because most of the times I’ve seen constructors they only contained self variables. I’ve tried to find an answer on the …

Total answers: 2