On Mon, Mar 21, 2016 at 6:05 PM, Steven D'Aprano wrote:
> On Tue, 22 Mar 2016 04:48 am, Ian Kelly wrote:
>
>> You don't actually need a metaclass for this:
>>
> class Desc:
>> ... def __get__(self, obj, type=None):
>> ... if not type._cached_value:
>> ... type._cached_v
> If a method call on any instance defines the return value for
> all instances, then this method likely should be a class method --
> and use a class attribute to store the result -- something like this:
>
> class C(object):
>
> _cache = {}
>
> @classmethod
> def f(cls, ...):
>
"Joseph L. Casale" writes:
> ...
> I need to cache the results of a method on a class across all instances.
If a method call on any instance defines the return value for
all instances, then this method likely should be a class method --
and use a class attribute to store the result -- something l
On Tue, Mar 22, 2016 at 11:05 AM, Steven D'Aprano wrote:
> But my favourite is to combine them:
>
>
> class Desc:
> def __get__(self, obj, type):
> sentinel = object() # guaranteed to be unique
> value = getattr(type, _cached_value, sentinel)
> if value is sentinel:
>
On Tue, 22 Mar 2016 04:48 am, Ian Kelly wrote:
> You don't actually need a metaclass for this:
>
class Desc:
> ... def __get__(self, obj, type=None):
> ... if not type._cached_value:
> ... type._cached_value = compute_value(type)
> ... return type._cached_valu
On Mon, Mar 21, 2016 at 10:54 AM, Chris Angelico wrote:
> On Tue, Mar 22, 2016 at 3:49 AM, Joseph L. Casale
> wrote:
>> Right, but _private refers to an api call that is expensive and may not even
>> be accessed,
>> so while I may new up three instances of Test across a, b and c, if none of
>>
On 03/21/2016 10:03 AM, Joseph L. Casale wrote:
One solution is to use descriptor protocol on the class, which means
using a metaclass. I'm not sure it's the best option, but it is an
option.
I will look at that, I wonder if however I am not over complicating it:
class Foo:
_bar = None
On Mon, Mar 21, 2016 at 10:36 AM, Steven D'Aprano wrote:
> On Tue, 22 Mar 2016 03:15 am, Ian Kelly wrote:
>> Why not do the same thing but using a class attribute instead of an
>> instance attribute?
>
> Properties don't work when called from a class:
Properties specifically do not, but descripto
> One solution is to use descriptor protocol on the class, which means
> using a metaclass. I'm not sure it's the best option, but it is an
> option.
I will look at that, I wonder if however I am not over complicating it:
class Foo:
_bar = None
@property
def expensive(self):
i
On Tue, Mar 22, 2016 at 3:49 AM, Joseph L. Casale
wrote:
> Right, but _private refers to an api call that is expensive and may not even
> be accessed,
> so while I may new up three instances of Test across a, b and c, if none of
> those end up
> accessing var x, it shouldn't get fetched. Without
> I think Joseph is using "static" in the Java sense of being associated with
> the class rather than an instance. (In Java, members of classes must be
> known at compile-time.)
Yup, so a single value on the class itself, not instance specific.
> But what you can do is have the property refer to
On Tue, 22 Mar 2016 03:15 am, Ian Kelly wrote:
> On Mon, Mar 21, 2016 at 9:38 AM, Joseph L. Casale
> wrote:
>> With non static properties, you can use a decorator that overwrites the
>> method on the instance with an attribute containing the methods return
>> effectively caching it.
>
> Can you
On Mon, Mar 21, 2016 at 9:38 AM, Joseph L. Casale
wrote:
> With non static properties, you can use a decorator that overwrites the
> method on the instance with an attribute containing the methods return
> effectively caching it.
Can you give an example of what you mean?
> What technique for a s
With non static properties, you can use a decorator that overwrites the
method on the instance with an attribute containing the methods return
effectively caching it.
What technique for a static property can be used to accomplish what the
descriptor protocol does?
I need to cache the results of a
14 matches
Mail list logo