initialization

The sqlalchemy hybrid_property can not be set during initialization, invalid keyword argument?

The sqlalchemy hybrid_property can not be set during initialization, invalid keyword argument? Question: I have a class of a sqlalchemy model like this: from sqlalchemy.ext.hybrid import hybrid_property class Connection(Model): name = dbColumn(db.String, nullable=False) provider = db.Column(db.String, nullable=True) version = db.Column(db.Integer, nullable=True) _connection_status = db.Column(‘connection_status’,db.String, nullable=True) @hybrid_property def connection_status(self) -> str: if not self._connection_status: self._connection_status="Connection_Not_Set" return …

Total answers: 1

2d array to two columns in dataframe

2d array to two columns in dataframe Question: I got the variable ‘v’ which is a 2d array: in = v out = array([[ 217.1, 252.5], [ 73. , 53. ], [ 83. , 827. ], …, [ 129. , 1214. ], [ 118.6, 908.2], [ 90. , 99.5]]) I have a dataframe with multiple …

Total answers: 3

Python – instance attribute defined outside __init__()

Python – instance attribute defined outside __init__() Question: I get the warning in question for the following code: from tkinter import * from tkinter import ttk class Autocomplete(Frame, object): def __init__(self, *args, **kwargs): super(Autocomplete, self).__init__(*args, **kwargs) self.list = [] def build(self, width, height, entries): # I get the warning for the following 8 lines: self._entries …

Total answers: 2

Multiple constructors: the Pythonic way?

Multiple constructors: the Pythonic way? Question: I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don’t pass data; just create an empty container In Java, I would create three constructors. Here’s …

Total answers: 7

What's the difference between super() and Parent class name?

What's the difference between super() and Parent class name? Question: Is there a difference between using super() and using the parent class name directly? For example: class Parent: def __init__(self): print(“In parent”) self.__a=10 class Child(Parent): def __init__(self): super().__init__() # using super() Parent.__init__(self) # using Parent class name c=Child() Is there internally a difference between super().__init__() …

Total answers: 2

Use an object method with the Initializer (Same line)

Use an object method with the Initializer (Same line) Question: I’m cleaning up a python object class, focusing mainly on how the object is created. The __init__ method creates a an empty dictionary that needs to be filled almost instantly. But this should NOT happen within the __init__, as the method used will vary widely. …

Total answers: 3

What can `__init__` do that `__new__` cannot?

What can `__init__` do that `__new__` cannot? Question: In Python, __new__ is used to initialize immutable types and __init__ typically initializes mutable types. If __init__ were removed from the language, what could no longer be done (easily)? For example, class A: def __init__(self, *, x, **kwargs): super().__init__(**kwargs) self.x = x class B(A): def __init__(self, y=2, …

Total answers: 3

how to declare uninitialized variable in class definition in python

how to declare uninitialized variable in class definition in python Question: I come from a MATLAB background. When I create class definitions, I can instantiate “empty” variable names and then later assign values or objects to them. I.e. classdef myclass < handle properties var1 var2 end end a = myClass; a.var1 = someOtherClassObject; How do …

Total answers: 3