So I have a class with a method, which takes string. Somethinkg like this:
class A():
def func(self, name):
# do some stuff with it
I have finite number of possible values, [val1, val2, val2] for example, All strings. I want to use them like this:
a = A()
a.val1() # actually a.func(val1)
I tried to combine decorators and setattr:
class A():
def func(self, val):
# do some stuff with it
def register(self, val):
def wrapper(self):
self.func(val)
setattr(self, val, wrapper)
So I can iterate through all possible values in run-time:
a = A()
for val in vals:
a.register(val)
And it has zero effect. Usually setattr adds new attribute with value None, but in this case nothing happens. Can somebody explain why it is this way and what can I do?
register() isn't a decorator, it's mostly just a "function factory" with side-effects. Also, as I said in a comment, setattr() needs to know what name to assigned to the value.
Here's a way to get your code to work:
class A():
def func(self, val):
# do some stuff with it
print('func({}) called'.format(val))
def register(self, val, name):
def wrapper():
self.func(val)
wrapper.__name__ = name
setattr(self, name, wrapper)
vals = 10, 20, 30
a = A()
for i, val in enumerate(vals, 1):
a.register(val, 'val'+str(i)) # Creates name argument.
a.val1() # -> func(10) called
a.val2() # -> func(20) called
Related
Given a class and a set of its methods - how can you determine from within that class which methods have been decorated with a certain decorator?
My goal is to basically get the actual values of the methods that are decorated, so something like:
class A():
def get_values(self):
...
# returned {'a-special-name': 1, 'b': 2}
#my_dec('a-special-name') # Ideally be able to also put optional name
def a(self):
return 1
#my_dec
def b(self):
return 2
Any idea on how to accomplish this?
Edit: this should also work on parent classes, that is, if A is a subclass of:
class B():
#my_dec
def c(self):
return 3
then get_values() of A instance should return {'a-special-name': 1, 'b': 2, 'c': 3} (order is irrelevant of course)
Edit: class based decorator that works but not with inheritance. Any idea how to make it work with inheritance but without having to decorate the class itself?
class my_dec(object):
def __init__(self, func, name=None):
self.func = func
self.name = name or func.__name__
self.func._some_flag = True
def __get__(self, instance, cls=None):
if instance is None:
return self
return self.func(instance)
If you can define the decorator yourself, then simply have it "mark" the method object in some way:
def my_dec(method):
method._this_be_decorated = True
return method
The class can then look for those marked methods; something like:
from inspect import isfunction
class A:
def get_values(self):
return filter(lambda i: isfunction(i) and hasattr(i, '_this_be_decorated'),
vars(type(self)).values())
This will return an iterable of function objects which you can process further as needed.
def my_dec(name):
if callable(name):
# name is callable – take its name
func = name # no make the code more readable
func.special_name = func.__name__
return func
else:
# name is the name to give – add an inner layer of functions
def inner(function_object):
function_object.special_name = name
return function_object
return inner
class A():
def get_values(self):
# return a dict of special name to call result mapping for every class member that has a special_name.
return {func.special_name: func(self) for func in self.__class__.__dict__.values() if hasattr(func, 'special_name')}
# returned {'a-special-name': 1, 'b': 2}
#my_dec('a-special-name') # Ideally be able to also put optional name
def a(self):
return 1
#my_dec
def b(self):
return 2
def no_dec(self):
return 42
should do what you want.
As deceze mentions, decorators can do whatever they want so there's no reliable generic answer. If you "own" the decorator you can add special properties to it's return value ie (Q&D py2.7 example):
def mydec(name=''):
# py27 hack - for py3 you want nonlocal instead
n = [name]
def innerdec(func):
# py27 hack - for py3 you want nonlocal instead
name = n[0] or func.__name__
def wrapper(*args, **kw):
print("in mydec.wrapper for {}".format(name))
return func(*args, **kw)
wrapper.ismydec = True # so we know this is decorated by mydec
wrapper.func = func # so we can get the original func
wrapper.name = name
return wrapper
return innerdec
def collect_dec(cls):
decorated = {}
for attname in dir(cls):
obj = getattr(cls, attname)
if getattr(obj, "ismydec", False):
decorated[obj.name] = obj.func
cls._decorated_funcs = decorated
return cls
#collect_dec
class A():
def get_values(self):
return {
name:func(self) for name, func in self._decorated_funcs.items()
}
#mydec('a-special-name') # Ideally be able to also put optional name
def a(self):
return 1
#mydec() # no name
def b(self):
return 2
a = A()
print(a.get_values())
Which outputs:
{'a-special-name': 1, 'b': 2}
Currently when I want to define a setter and leave getter alone I do this:
#property
def my_property(self):
return self._my_property
#my_property.setter
def my_property(self, value):
value.do_some_magic()
self._my_property = value
Is there any way to make it shorter? I'd like to skip this part as it always look the same:
#property
def my_property(self):
return self._my_property
There's no out of the box solution, but you can try something like this:
def defprop(name):
def getter(self):
return getattr(self, name)
return property(getter)
class C(object):
# ...
my_dictionary = defprop('_my_dictionary')
# ...
That does not save you that many keystrokes though, you still have to duplicate the attribute name. Besides it's less explicit.
Update: after thinking a bit, I've come up with this descriptor-based hackish trick (disclaimer: this is done just for a demonstration, I don't imply it's a good practice unless you have a damn good reason to do so):
class with_default_getter(object):
def __init__(self, func):
self._attr_name = '_{0.__name__}'.format(func)
self._setter = func
def __get__(self, obj, type):
return getattr(obj, self._attr_name)
def __set__(self, obj, value):
return self._setter(obj, value)
Usage:
class C(object):
#with_default_getter
def my_property(self, value):
print 'setting %s'
self._my_property = value
>>> c = C()
>>> c.my_property = 123
setting 123
>>> c.my_property
123
This is pretty much the same as #georg suggests, just unfolds the implementation down to descriptors.
You can make a decorator that auto-creates the getter, following the underscores convention:
def setter(fn):
def _get(self):
return getattr(self, '_' + fn.__name__)
def _set(self, val):
return fn(self, val)
return property(_get, _set)
or more concisely, if you like this style more:
def setter(fn):
return property(
lambda self: getattr(self, '_' + fn.__name__),
fn)
Usage:
class X(object):
#setter
def my_property(self, value):
self._my_property = value + 1
x = X()
x.my_property = 42
print x.my_property # 43
There is no shortcut that I am aware of- remember explicit is better than implicit (from the Zen of python).
It could be that in your code so far, a property is always like that - but you could at some point write a a property getter which fetches an entirely calculated value - in which case your property getter and setter wont look like that at all.
Haveing said that you could write a wrapper which provides those simple default methods as part of the wrapper, if you wish.
def set_my_property(self, value):
value.do_some_magic()
self._my_property = value
my_property = property(fset=set_my_property)
I'm just getting to grips with decorators in Python and using them to add callbacks to some instance variables using the following simple pattern:
class A(object):
def __init__(self):
self._var = 0
self.var_callbacks = []
#property
def var(self):
return self._var
#var.setter
def var(self, x):
self._var = x
for f in self.var_callbacks:
f(x)
The property decorator is a neat way of allowing me to introduce callbacks where necessary without changing the class interface. However, after the third or fourth variable it's making the code a bit repetitive.
Is there a way to refactor this pattern into something along the following:
class A(object):
def __init__(self):
self.var = 0
enable_callback(self, 'var', 'var_callbacks')
You'll need to set the property on the class (since it is a descriptor), so using a enable_callback call in the initializer is not going to work.
You could use a class decorator to set the properties from a pattern:
def callback_properties(callbacks_attribute, *names):
def create_callback_property(name):
def getter(self):
return getattr(self, '_' + name)
def setter(self, value):
setattr(self, '_' + name, value)
for f in getattr(self, callbacks_attribute):
f(value)
return property(getter, setter)
def add_callback_properties(cls):
for name in names:
setattr(cls, name, create_callback_property(name)
return cls
return add_callback_properties
Then use that as:
#add_callback_properties('var_callbacks', 'var1', 'var2')
class A(object):
# everything else
Have a look at the Python descriptor protocol. In essence, you can define a class that handles the getting, setting and deleting of a property. So you could define a descriptor that runs your callbacks on setting the attribute.
Descriptors are regular classes, and can be parameterized. So you could implement a descriptor that takes the destination variable its constructor. Something like the following:
class A(object):
var = CallbackDescriptor('var')
foo = CallbackDescriptor('foo')
Situation
Similar to this question, I want to replace a property. Unlike that question, I do not want to override it in a sub-class. I want to replace it in the init and in the property itself for efficiency, so that it doesn't have to call a function which calculates the value each time the property is called.
I have a class which has a property on it. The constructor may take the value of the property. If it is passed the value, I want to replace the property with the value (not just set the property). This is because the property itself calculates the value, which is an expensive operation. Similarly, I want to replace the property with the value calculated by the property once it has been calculated, so that future calls to the property do not have to re-calculate:
class MyClass(object):
def __init__(self, someVar=None):
if someVar is not None: self.someVar = someVar
#property
def someVar(self):
self.someVar = calc_some_var()
return self.someVar
Problem
The above code does not work because doing self.someVar = does not replace the someVar function. It tries to call the property's setter, which is not defined.
Potential Solution
I know I can achieve the same thing in a slightly different way as follows:
class MyClass(object):
def __init__(self, someVar=None):
self._someVar = someVar
#property
def someVar(self):
if self._someVar is None:
self._someVar = calc_some_var()
return self._someVar
This will be marginally less efficient as it will have to check for None every time the property is called. The application is performance critical, so this may or may not be good enough.
Question
Is there a way to replace a property on an instance of a class? How much more efficient would it be if I was able to do this (i.e. avoiding a None check and a function call)?
What you are looking for is Denis Otkidach's excellent CachedAttribute:
class CachedAttribute(object):
'''Computes attribute value and caches it in the instance.
From the Python Cookbook (Denis Otkidach)
This decorator allows you to create a property which can be computed once and
accessed many times. Sort of like memoization.
'''
def __init__(self, method, name=None):
# record the unbound-method and the name
self.method = method
self.name = name or method.__name__
self.__doc__ = method.__doc__
def __get__(self, inst, cls):
# self: <__main__.cache object at 0xb781340c>
# inst: <__main__.Foo object at 0xb781348c>
# cls: <class '__main__.Foo'>
if inst is None:
# instance attribute accessed on class, return self
# You get here if you write `Foo.bar`
return self
# compute, cache and return the instance's attribute value
result = self.method(inst)
# setattr redefines the instance's attribute so this doesn't get called again
setattr(inst, self.name, result)
return result
It can be used like this:
def demo_cache():
class Foo(object):
#CachedAttribute
def bar(self):
print 'Calculating self.bar'
return 42
foo=Foo()
print(foo.bar)
# Calculating self.bar
# 42
Notice that accessing foo.bar subsequent times does not call the getter function. (Calculating self.bar is not printed.)
print(foo.bar)
# 42
foo.bar=1
print(foo.bar)
# 1
Deleting foo.bar from foo.__dict__ re-exposes the property defined in Foo.
Thus, calling foo.bar again recalculates the value again.
del foo.bar
print(foo.bar)
# Calculating self.bar
# 42
demo_cache()
The decorator was published in the Python Cookbook and can also be found on ActiveState.
This is efficient because although the property exists in the class's __dict__, after computation, an attribute of the same name is created in the instance's __dict__. Python's attribute lookup rules gives precedence to the attribute in the instance's __dict__, so the property in class becomes effectively overridden.
Sure, you can set the attribute in the private dictionary of the class instance, which takes precedence before calling the property function foo (which is in the static dictionary A.__dict__)
class A:
def __init__(self):
self._foo = 5
self.__dict__['foo'] = 10
#property
def foo(self):
return self._foo
assert A().foo == 10
If you want to reset again to work on the property, just del self.__dict__['foo']
class MaskingProperty():
def __init__(self, fget=None, name=None, doc=None):
self.fget = fget
if fget is not None:
self.name = fget.__name__
self.__doc__ = doc or fget.__doc__
def __call__(self, func):
self.fget = func
self.name = func.__name__
if not self.__doc__:
self.__doc__ = func.__doc__
return self
def __get__(self, instance, cls):
if instance is None:
return self
if self.fget is None:
raise AttributeError("seriously confused attribute <%s.%s>" % (cls, self.name))
result = self.fget(instance)
setattr(instance, self.name, result)
return result
This is basically the same as Denis Otkidach's CachedAttribute, but slightly more robust in that it allows either:
#MaskingProperty
def spam(self):
...
or
#MaskingProperty() # notice the parens! ;)
def spam(self):
...
You can change what code a function has by replacing the functions's __code__object with the __code__ object from another function.
Here is a decorator function that I created to do just that for you. Feel free to modify it as you see fit. The big thing to remember though is that the both functions need to have the same number of 'free variables' to be swapped like this. This can easily be done by using nonlocal to force it (as shown below).
NULL = object()
def makeProperty(variable = None, default = NULL, defaultVariable = None):
"""Crates a property using the decorated function as the getter.
The docstring of the decorated function becomes the docstring for the property.
variable (str) - The name of the variable in 'self' to use for the property
- If None: uses the name of 'function' prefixed by an underscore
default (any) - What value to initialize 'variable' in 'self' as if it does not yet exist
- If NULL: Checks for a kwarg in 'function' that matches 'defaultVariable'
defaultVariable (str) - The name of a kwarg in 'function' to use for 'default'
- If None: Uses "default"
Note: this must be a kwarg, not an arg with a default; this means it must appear after *
___________________________________________________________
Example Use:
class Test():
#makeProperty()
def x(self, value, *, default = 0):
'''Lorem ipsum'''
return f"The value is {value}"
test = Test()
print(test.x) #The value is 0
test.x = 1
print(test.x) #The value is 1
Equivalent Use:
#makeProperty(defaultVariable = "someKwarg")
def x(self, value, *, someKwarg = 0):
Equivalent Use:
#makeProperty(default = 0)
def x(self, value):
___________________________________________________________
"""
def decorator(function):
_variable = variable or f"_{function.__name__}"
if (default is not NULL):
_default = default
elif (function.__kwdefaults__ is not None):
_default = function.__kwdefaults__.get(defaultVariable or "default")
else:
_default = None
def fget(self):
nonlocal fget_runOnce, fget, fset, _default #Both functions must have the same number of 'free variables' to replace __code__
return getattr(self, _variable)
def fget_runOnce(self):
if (not hasattr(self, _variable)):
fset(self, _default)
fget_runOnce.__code__ = fget.__code__
return getattr(self, _variable)
def fset(self, value):
setattr(self, _variable, function(self, value))
def fdel(self):
delattr(self, _variable)
return property(fget_runOnce, fset, fdel, function.__doc__)
return decorator
I want to create a decorator that works like a property, only it calls the decorated function only once, and on subsequent calls always return the result of the first call. An example:
def SomeClass(object):
#LazilyInitializedProperty
def foo(self):
print "Now initializing"
return 5
>>> x = SomeClass()
>>> x.foo
Now initializing
5
>>> x.foo
5
My idea was to write a custom decorator for this. So i started, and this is how far I came:
class LazilyInitializedProperty(object):
def __init__(self, function):
self._function = function
def __set__(self, obj, value):
raise AttributeError("This property is read-only")
def __get__(self, obj, type):
# problem: where to store the value once we have calculated it?
As you can see, I do not know where to store the cached value. The simplest solution seems to be to just maintain a dictionary, but I am wondering if there is a more elegant solution for this.
EDIT Sorry for that, I forgot to mention that I want the property to be read-only.
Denis Otkidach's CachedAttribute is a method decorator which makes attributes lazy (computed once, accessible many). To make it also read-only, I added a __set__ method. To retain the ability to recalculate (see below) I added a __delete__ method:
class ReadOnlyCachedAttribute(object):
'''Computes attribute value and caches it in the instance.
Source: Python Cookbook
Author: Denis Otkidach https://stackoverflow.com/users/168352/denis-otkidach
This decorator allows you to create a property which can be computed once and
accessed many times. Sort of like memoization
'''
def __init__(self, method, name=None):
self.method = method
self.name = name or method.__name__
self.__doc__ = method.__doc__
def __get__(self, inst, cls):
if inst is None:
return self
elif self.name in inst.__dict__:
return inst.__dict__[self.name]
else:
result = self.method(inst)
inst.__dict__[self.name]=result
return result
def __set__(self, inst, value):
raise AttributeError("This property is read-only")
def __delete__(self,inst):
del inst.__dict__[self.name]
For example:
if __name__=='__main__':
class Foo(object):
#ReadOnlyCachedAttribute
# #read_only_lazyprop
def bar(self):
print 'Calculating self.bar'
return 42
foo=Foo()
print(foo.bar)
# Calculating self.bar
# 42
print(foo.bar)
# 42
try:
foo.bar=1
except AttributeError as err:
print(err)
# This property is read-only
del(foo.bar)
print(foo.bar)
# Calculating self.bar
# 42
One of the beautiful things about CachedAttribute (and
ReadOnlyCachedAttribute) is that if you del foo.bar, then the next time you
access foo.bar, the value is re-calculated. (This magic is made possible by
the fact that del foo.bar removes 'bar' from foo.__dict__ but the property
bar remains in Foo.__dict__.)
If you don't need or don't want this ability to recalculate,
then the following (based on Mike Boers' lazyprop) is a simpler way to make a read-only lazy property.
def read_only_lazyprop(fn):
attr_name = '_lazy_' + fn.__name__
#property
def _lazyprop(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, fn(self))
return getattr(self, attr_name)
#_lazyprop.setter
def _lazyprop(self,value):
raise AttributeError("This property is read-only")
return _lazyprop