Re: [Tutor] classproperty: readonly and inheritance - not more needed
Now the "not read-only" part: Foo.my_prop = "whatever" Foo.my_prop 'whatever' You now have a string attribute, the property is lost. Methods behave the same way and it's generally not a problem, but you should at least be aware of this behaviour. Yes, now I understand you. Thank you Regards, Thomas Güttler -- Thomas Guettler http://www.thomas-guettler.de/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] classproperty: readonly and inheritance - not more needed
Thomas Güttler wrote: > > > Am 20.04.2017 um 14:26 schrieb Steven D'Aprano: >> On Thu, Apr 20, 2017 at 10:39:57AM +0200, Thomas Güttler wrote: >> - its hard to get classproperty to work right. >>> >>> What is "righ"? >>> >>> In my case a read-only classproperty is enough. Inheritance should be >>> supported. >>> >>> I don't have a usecase for a setter. >> >> The standard library is not just for you :-) >> >> If Peter's solution is "good enough" for you, then great, go ahead and >> use it. But beware: of the two implementations I know, you cannot have >> both: >> >> - access from instances; >> - read-only property; >> >> You can have access from instances, but then the classproperty is not >> read-only. Or you can have read-only access, but only from the class >> object. > > I can't follow what you. What do you mean with "... is not read-only". > > This snippet works fine: > > {{{ > > class classproperty(object): > def __init__(self, f): > self.f = f > def __get__(self, obj, owner): > return self.f(owner) > > class Foo(object): > @classproperty > def my_prop(cls): > return 42 > > print Foo.my_prop > > print Foo().my_prop > }}} > > Regards, >Thomas > Python 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class classproperty(object): ... def __init__(self, f): ... self.f = f ... def __get__(self, obj, owner): ... return self.f(owner) ... >>> class Foo(object): ... @classproperty ... def my_prop(cls): ... print "calculating..." ... return 42 ... >>> Foo.my_prop calculating... 42 Now the "not read-only" part: >>> Foo.my_prop = "whatever" >>> Foo.my_prop 'whatever' You now have a string attribute, the property is lost. Methods behave the same way and it's generally not a problem, but you should at least be aware of this behaviour. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] classproperty: readonly and inheritance - not more needed
Am 20.04.2017 um 14:26 schrieb Steven D'Aprano: On Thu, Apr 20, 2017 at 10:39:57AM +0200, Thomas Güttler wrote: - its hard to get classproperty to work right. What is "righ"? In my case a read-only classproperty is enough. Inheritance should be supported. I don't have a usecase for a setter. The standard library is not just for you :-) If Peter's solution is "good enough" for you, then great, go ahead and use it. But beware: of the two implementations I know, you cannot have both: - access from instances; - read-only property; You can have access from instances, but then the classproperty is not read-only. Or you can have read-only access, but only from the class object. I can't follow what you. What do you mean with "... is not read-only". This snippet works fine: {{{ class classproperty(object): def __init__(self, f): self.f = f def __get__(self, obj, owner): return self.f(owner) class Foo(object): @classproperty def my_prop(cls): return 42 print Foo.my_prop print Foo().my_prop }}} Regards, Thomas -- Thomas Guettler http://www.thomas-guettler.de/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] classproperty: readonly and inheritance - not more needed
On Thu, Apr 20, 2017 at 10:39:57AM +0200, Thomas Güttler wrote: > >- its hard to get classproperty to work right. > > What is "righ"? > > In my case a read-only classproperty is enough. Inheritance should be > supported. > > I don't have a usecase for a setter. The standard library is not just for you :-) If Peter's solution is "good enough" for you, then great, go ahead and use it. But beware: of the two implementations I know, you cannot have both: - access from instances; - read-only property; You can have access from instances, but then the classproperty is not read-only. Or you can have read-only access, but only from the class object. Although I haven't studied Eryksun's solution yet, he may have found a work-around. Good luck! -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor