properties

when to use getter and setter with property?

when to use getter and setter with property? Question: When should you use a property with getters/setters? It is not pythonic or wrong to not use a property with getters and setters? Should or shouldn’t I write it with a property? Examples: class Person: def __init__(self, firstname, lastname, age): self.firstname = firstname self.lastname = lastname …

Total answers: 4

python colorama print all colors

python colorama print all colors Question: I am new to learning Python, and I came across colorama. As a test project, I wanted to print out all the available colors in colorama. from colorama import Fore from colorama import init as colorama_init colorama_init(autoreset=True) colors = [x for x in dir(Fore) if x[0] != "_"] for …

Total answers: 2

Best way to 'intelligently' reset memoized property values in Python when dependencies change

Best way to 'intelligently' reset memoized property values in Python when dependencies change Question: I’m writing a class with various attributes that I only want to calculate when necessary (lazy evaluation). However, more importantly, I want to make sure that ‘stale’ values are not returned if any of the attributes that their calculation depended on …

Total answers: 5

How to combine dataclass, property, and lru_cache

How to combine dataclass, property, and lru_cache Question: I’m trying to combine dataclasses, properties and lru_caches for some computational science code: from dataclasses import dataclass from typing import Any from functools import lru_cache @dataclass class F: a: Any = 1 b: Any = 2 c: Any = 3 @property @lru_cache(1) def d(self): print(‘Computing d’) return …

Total answers: 1

TypeError: 'str' object is not callable using Selenium through Python

TypeError: 'str' object is not callable using Selenium through Python Question: When I try to do code shown below I get error : TypeError: ‘str’ object is not callable email2_elem = driver.find_element_by_xpath(“/html/body/div[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/input[1]”).text() Asked By: szymond45 || Source Answers: text is a property, not a function. Use it without () element.text As a side note, absolute …

Total answers: 3

Creating properties dynamically that point to an attribute's properties

Creating properties dynamically that point to an attribute's properties Question: I want to make properties from an attribute of my class accessible directly through the instance of the class (without inheriting from it). So basically if I have: class A: @property def foo(self): print(“foo”) @property def bar(self): print(“bar”) class B: def __init__(self): self._a = A() …

Total answers: 1

Python property descriptor design: why copy rather than mutate?

Python property descriptor design: why copy rather than mutate? Question: I was looking at how Python implements the property descriptor internally. According to the docs property() is implemented in terms of the descriptor protocol, reproducing it here for convenience: class Property(object): “Emulate PyProperty_Type() in Objects/descrobject.c” def __init__(self, fget=None, fset=None, fdel=None, doc=None): self.fget = fget self.fset …

Total answers: 3

How to write to an abstract property in Python 3.4+?

How to write to an abstract property in Python 3.4+ Question: In Python 3.6, Let’s say I have an abstract class MyAbstractClass from abc import ABC, abstractmethod class MyAbstractClass(ABC): @property @abstractmethod def myProperty(self): pass and a class MyInstantiatableClass inherit from it. So how do I write to the property myProperty on instantiation of an object …

Total answers: 2

AttributeErrors: undesired interaction between @property and __getattr__

AttributeErrors: undesired interaction between @property and __getattr__ Question: I have a problem with AttributeErrors raised in a @property in combination with __getattr__() in python: Example code: >>> def deeply_nested_factory_fn(): … a = 2 … return a.invalid_attr … >>> class Test(object): … def __getattr__(self, name): … if name == ‘abc’: … return ‘abc’ … raise AttributeError(“‘Test’ …

Total answers: 3

Python property does not use setter when setting the value in the constructor

Python property does not use setter when setting the value in the constructor Question: I have a class with a constructor and a couple of properties class Foo(object): def __init__(self, value1): self._value1 = value1 @property def value1(self): return self._value1 @property.setter def value1(self, value): assert value == 1 self._value1 = value Now when I set value1 …

Total answers: 2