properties

python property class namespace confusion

python property class namespace confusion Question: I am confused about use of property class with regard to references to the fset/fget/fdel functions and in which namespaces they live. The behavior is different depending on whether I use property as a decorator or a helper function. Why do duplicate vars in class and instance namespaces impact …

Total answers: 2

Automatically add methods to a class in Python

Automatically add methods to a class in Python Question: I’m trying to create a custom class to handle timeseries related to various objects. They all inherit from a base class called Timeseries, which looks like this class Timeseries: def __init__(self, t_max: int): self.t_max = t_max self.current_t = 0 def update(self): if self.current_t == self.t_max: raise …

Total answers: 1

problem defining class property in python

problem defining class property in python Question: I’m new to python programming, I’m trying to encapsulate all property checking of a size in the class object below but one of them is not working (checking to see if withdraw is bigger than amount of books inside the shelf): class Shelf: def __init__(self, capacity=12): self.capacity = …

Total answers: 1

does the @property decorator function as a getter?

does the @property decorator function as a getter? Question: i am new to python and i’m trying to understand the use of the ‘getter’. it’s use case is not obvious to me. if i use a property decorator on a method and im able to return a certain value, what exactly would i use ‘getter’ …

Total answers: 2

How to add a property to a child dataclass before a default property?

How to add a property to a child dataclass before a default property? Question: Let’s suppose that I have a dataclass A. The dataclass A contains some property with a default value. Let’s suppose that I want to extend that dataclass with a dataclass B. I need to add some non-default property to the dataclass …

Total answers: 1

Mocking an Async Property in Python

Mocking an Async Property in Python Question: The below example class has a property bar that is awaitable, as in async_main() because it (theoretically) does some IO work before returning the answer to everything. class Foo: @property async def bar(self): return 42 async def async_main(): f = Foo() print(await f.bar) I’m having trouble testing it, …

Total answers: 1

How to create property methods in django models dynamically?

How to create property methods in django models dynamically? Question: I am creating property methods for every model where the model attribute includes ImageField or FileField. So, I decided to make an abstract model where I check the fields in the model and if there are any ImageField and FileField in the model the property …

Total answers: 1

Call to Python lambda function without parenthesis

Call to Python lambda function without parenthesis Question: I have a dictionary that each value is a lambda function with a class getter function. class test: def __init__( self, filename: str, ): self.filename = filename @property def read_file(self): return read_file(self.filename) # Return pandas dataframe func_1 = test(filename_1) func_2 = test(filename_2) ret = {} ret ["key_1"] …

Total answers: 2