On Sun, Dec 19, 2021 at 5:41 AM Serhiy Storchaka
wrote:
So you can use a combination of @classmethod and @property.
>
> class Data:
> @classmethod
> @property
> def imagesTotal(cls):
> return 10
And this would be more useful anyway: if you want a class property, it may
well
18.12.21 13:18, m...@chenjt.com пише:
> It might be a good idea to use "@staticproperty" to solve this problem.
> "@staticproperty" is a decorators, it mix the @staticmethod and @property.
> Then the static property has getter and setter.
classmethod supersedes staticmethod. It was not clearly
Thank everyone!
I just want to use a class that holds global variables and has a value that can
be easily calculated. So I thought it would be more elegant to use
"@staticproperty".
These is just my personal thought and not necessary. Now you've given me the
solution.
On Sun, Dec 19, 2021 at 8:10 AM Ethan Furman wrote:
>
> By way of correcting misconceptions:
>
> On 12/18/21 8:39 AM, Chris Angelico wrote:
> >
> > I'm not sure that this is actually possible the way you're doing it.
> > The descriptor protocol (which is what makes properties work) won't
> >
By way of correcting misconceptions:
On 12/18/21 8:39 AM, Chris Angelico wrote:
>
> I'm not sure that this is actually possible the way you're doing it.
> The descriptor protocol (which is what makes properties work) won't
> apply when you're looking up statically.
On 12/18/21 9:19 AM,
If the goal is to create a "property" that behaves like such but doesn't
receive "self" as a parameter, the straightforward way to do so would be a
simple descriptor.
class StaticProperty:
def __init__(self, fget=None, fset=None, fdel=None):
self.fget = fget
self.fset = fset
All that is needed is a descriptor which calls the decorated functiosn
without any parameters.
This could serve to use classes as namespaces for "live" globals - if one
will do some sort
of reactive programing, it might even find some use.
A descriptor with a for that would be something like:
I'm confused about what a staticproperty would even be.
Usually, properties are a way to provide an interface that "looks like" a
simple attribute, but does some computation under the hood. But that
computation usually requires instance data to do its thing -- so a static
one wouldn't be useful.
On Sun, Dec 19, 2021 at 3:31 AM wrote:
>
> In the following situations:
>
>
> class Data(object):
> @staticmethod
> @property
> def imagesTotal():
> return 10
>
> print(Data.imagesTotal)
>
>
> The "print(Data.imagesTotal)" can't print "10", it print " 0x...>".
>
> It might be