Open Trung0246 opened 9 months ago
Can you give a high level explanation with usage examples of what you are trying to do rather than ask about what you think is the solution. I need to understand what your goal is to be able to suggest the best way, or even if you should be using wrapt at all, since it isn't a magic solution for everything and often not even needed.
Anyway, what you are asking for sort of mirrors what some people have asked for before but never had a good solution. That is, maintain an attribute on the wrapper which isn't propagated through to the wrapped object, but not require a _self_
prefix.
Now for whatever reason an idea of how to solve it just popped into my head for some reason. Rather funny that it has taken me so long to realise this could work.
import wrapt
class Wrapper(wrapt.ObjectProxy):
# Declare class attribute so lookup will work even
# though going to use the attribute on the instance.
data = "wrapper"
def __init__(self, wrapped, data=None):
super().__init__(wrapped)
# Since setting on instance, will convert the class
# attribute to an instance variable. The original
# class attribute declaration means it will not
# be on the wrapped object.
self.data = data
class Wrapped: pass
o = Wrapped()
p1 = Wrapper(o, "p1")
p2 = Wrapper(o, "p2")
print(Wrapper.data)
print(p1.data)
print(p2.data)
p1.data = "p1-update"
p2.data = "p2-update"
print(Wrapper.data)
print(p1.data)
print(p2.data)
print(dir(o))
o.data = "wrapped"
print(o.data)
print(Wrapper.data)
print(p1.data)
print(p2.data)
print(dir(o))
Which yields:
wrapper
p1
p2
wrapper
p1-update
p2-update
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
wrapped
wrapper
p1-update
p2-update
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'data']
Thanks. It's kinda embarrassing tho since I'm implementing some monkey patching for data hijack through a python system. The entire system have some sort of data passing around, each data can be handled with some sort of callback. The thing is the callbacks are kind of limited for my need but I found this way to workaround with it, which is to to associate some data with since modifying the system itself is basically a game of data chasing.
I guess the linked issue is a dupe of this then?
Can't say whether the other issue which I pointed at this one is a dupe or not since ultimately don't fully understand what your or other posters goals really are.
Anyway, here is another way of tricking the Python attribute lookup mechanism.
class Wrapper(wrapt.ObjectProxy):
# Declare a slot for the attribute so lookup will work against
# the instance and not go through to the wrapped object.
__slots__ = ["data"]
def __init__(self, wrapped, data=None):
super().__init__(wrapped)
self.data = data
IOW. Instead of defining a dummy class attribute, add __slots__
array and list data
in it.
Current my implementation works like this:
But the moment when
Wrapper
isWrapper(123)
or some other primitive values, this becomes unusable. I have also attempted to do the following:But unsure if it's reliable for the case when
self
itself havedata
or_self_dict
attribute when looking atProxy Object Attributes
section in the docs.