Giter Club home page Giter Club logo

Comments (3)

GrahamDumpleton avatar GrahamDumpleton commented on July 26, 2024

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.

Trung0246 avatar Trung0246 commented on July 26, 2024

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.

GrahamDumpleton avatar GrahamDumpleton commented on July 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.