Like I always did before 3.9, nonetheless: a custom "property" rewrite.
The problem is, "property" does a lot of things, and if one will need everything its in there, it is a lot of code.
I guess it is possible to just subclass property itself, so that we can get an extra .class_getter decorator.
A class setter, obviously, would involve either a custom metaclass or an especialisation of __setattr__.
Let's see if I can come with a reasonably short classproperty.
[after tinkering a bit]
So, it turns out simply inheriting property and adding a decorator for a "class getter" is not easily feasible - "property" is not written with subclassing and expanding its functionality in mind.
Therefore, the "easy" thing, and subset is to write a custom descriptor decorator, which will just convert a single method into a classgetter - and no set, del or inheritance support at all.
On the other hand, the code is short and simple:
class classproperty:
def __init__(self, func):
self.fget = func
def __get__(self, instance, owner):
return self.fget(owner)
And this simply works as expected:
In [19]: class A:
...: @classproperty
...: def test(cls):
...: return f"property of {cls.__name__}"
...:
In [20]: A.test
Out[20]: 'property of A'
Another way, if one wants to go all the way to have a class attribute setter, it is a matter of writing a plain property on a custom metaclass (which can exist just for holding the desired properties). This approach however will render the properties invisible on the instances - they will work only on the class itself:
In [22]: class MetaA(type):
...: @property
...: def test(cls):
...: return cls._test
...: @test.setter
...: def test(cls, value):
...: cls._test = value.upper()
...:
In [23]: class A(metaclass=MetaA):
...: pass
...:
In [24]: A.test = "hello world!"
In [25]: A.test
Out[25]: 'HELLO WORLD!'
In [26]: A().test
--------------------------------------------------------------
...
AttributeError: 'A' object has no attribute
Answer from jsbueno on Stack OverflowLike I always did before 3.9, nonetheless: a custom "property" rewrite.
The problem is, "property" does a lot of things, and if one will need everything its in there, it is a lot of code.
I guess it is possible to just subclass property itself, so that we can get an extra .class_getter decorator.
A class setter, obviously, would involve either a custom metaclass or an especialisation of __setattr__.
Let's see if I can come with a reasonably short classproperty.
[after tinkering a bit]
So, it turns out simply inheriting property and adding a decorator for a "class getter" is not easily feasible - "property" is not written with subclassing and expanding its functionality in mind.
Therefore, the "easy" thing, and subset is to write a custom descriptor decorator, which will just convert a single method into a classgetter - and no set, del or inheritance support at all.
On the other hand, the code is short and simple:
class classproperty:
def __init__(self, func):
self.fget = func
def __get__(self, instance, owner):
return self.fget(owner)
And this simply works as expected:
In [19]: class A:
...: @classproperty
...: def test(cls):
...: return f"property of {cls.__name__}"
...:
In [20]: A.test
Out[20]: 'property of A'
Another way, if one wants to go all the way to have a class attribute setter, it is a matter of writing a plain property on a custom metaclass (which can exist just for holding the desired properties). This approach however will render the properties invisible on the instances - they will work only on the class itself:
In [22]: class MetaA(type):
...: @property
...: def test(cls):
...: return cls._test
...: @test.setter
...: def test(cls, value):
...: cls._test = value.upper()
...:
In [23]: class A(metaclass=MetaA):
...: pass
...:
In [24]: A.test = "hello world!"
In [25]: A.test
Out[25]: 'HELLO WORLD!'
In [26]: A().test
--------------------------------------------------------------
...
AttributeError: 'A' object has no attribute
I think this is a very good question, and I wish it had a better answer. My favourite approach that I was able to find was using __init_subclass__ from this answer.
classmethod properties are removed in Python 3.13
0.981 `@classmethod` and `@property` can no longer be chained
Creation of dynamic property deprecated
3.13 class properties
In 3.13
@classmethod @property def func...
Stopped working. Why was this functionally removed? What was the goal of removing it or what under the hood changed that made this impossible? Is there any alternative or workaround?
» pip install Deprecated
Hi im creating dynamic property for my class objects for
example i assign database column to my Model dynamically
now all of this is deprecated how can i fix this problem?