Comments (3)
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']
from wrapt.
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?
from wrapt.
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.
from wrapt.
Related Issues (20)
- ObjectProxy does not play well with GenericAlias, such as isinstance(proxy, Dict) HOT 12
- RFE: is it possible to start making github releases?🤔 HOT 2
- Publish cp312 wheel HOT 6
- Release 1.14.1 Py3.11 wheels HOT 7
- __doc__ property HOT 9
- Request to be able to import both py and c wrappers HOT 8
- Unable to install wrapt 1.14.1 via poetry HOT 2
- Accessing ObjectProxy __dict__ HOT 5
- Update to setup.cfg potentially required HOT 4
- Documentation isn't building? HOT 6
- Accessing a class attribute that is a wrapt wrapped function will try and bind the function. HOT 4
- pydevd error when debugging with wrapt HOT 30
- Add type hint annotations for user-facing code HOT 2
- classmethod tests fail with Python 3.13 (Python reverted to pre-3.9 behavior) HOT 2
- 1.16.0: pytest fails
- How to use adapter factory to change signature depending on instance HOT 9
- Accessing a class attribute that is a wrapped function raises an AttributeError
- RFE: remove use `PY2` and `PY3` in the module code HOT 1
- Is it possible to perform `json.dumps(obj_proxy)`? HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wrapt.