[Python-ideas] Decorators for variables
Eric Fahlgren
ericfahlgren at gmail.com
Sun Apr 3 10:14:32 EDT 2016
On Saturday, April 02, 2016 23:33, Chris Angelico wrote:
> On Sun, Apr 3, 2016 at 4:05 PM, Matthias welp <boekewurm at gmail.com> wrote:
> > currently changes behaviour depending on what kind of scope it is
> > located in (class description, any other scope), while decorators (for
> > functions at
> > least) work in every scope I can think of. I think that is strange,
> > and that it should just be the same everywhere.
>
> Can you explain - or, preferably, demonstrate - the difference you're talking about here?
I can sort of see it, like this?
class C():
def __init__(self):
self.x = 99
@property
def f(self):
return self.x
x = 101
@property
def f(namespace):
return namespace.x
c = C()
print(c.x)
99
print(c.f)
99
print(C.f.fget(c))
99
print(x)
101
Here's the inconsistency, should implicitly bind to a "global namespace object".
print(f)
<property object at 0x00000000023FB6D8>
In other words, something like this:
class GNS:
def __getattr__(self, name):
prop = globals().get(name)
if isinstance(prop, property):
return prop.fget(self)
return prop
global_namespace = GNS()
print(x is global_namespace.x, "should be 'True'")
True should be 'True'
print(global_namespace.f)
101
More information about the Python-ideas
mailing list