Skip to content

Object Watchdog uses existing instance instead of creating new instances #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ghost opened this issue Jun 17, 2021 · 6 comments
Open

Comments

@ghost
Copy link

ghost commented Jun 17, 2021

Imagine having the following snippet:

class A(metaclass=ObjectWatchdog):
	def __init__(self, number: int):
		self.number = number

a = A(1)
b = A(2)

What do we expect? We expect a.number == 1 and b.number == 2. However, when printing the a.number we see 2 (!). Adding a print statement before setting the number at initialization level (print(hasattr(self, "number"))) reveals that self.number is already set on instance b.

@yanongena
Copy link

yanongena commented Jul 25, 2021

Is there a fix for this? I have the same issue which makes the package pretty much useless...
It's a great package but this issue is really a big issue.

@cr0hn
Copy link
Owner

cr0hn commented Jul 26, 2021

Not yet. I have the issue in the queue, but other projects with sponsorship have preference

@yanongena
Copy link

yanongena commented Jul 26, 2021

I've been doing a bit of debugging and think I found the issue.
Because you are replacing the setattr method from a metaclass, you are actually replacing the class function so each instance has the same reference to that method so although we might have different objects, they point to the same function so that's why when setting an attribute on one object, also sets it on the other objects.

I've created my own version which doesn't use metaclass but regular inheritence. This works great.
Just wanted to share, just in case :)

class DataObserver:

    __observableAttrs__ = []
    __observers = []

    def __setattr__(self, key, value):
        self.__dict__[key] = value
        if key in self.__observableAttrs__:
            for observer in self.__observers:
                observer(self, key, value)


    def addObserver(self, fn):
        self.__observers.append(fn)

@idotzang
Copy link

idotzang commented Jul 1, 2022

This package seems to do exactly what I need, but this is indeed a major issue.
Any timeline to solving this?
Thank you

@saurabh0719
Copy link

This package seems to do exactly what I need, but this is indeed a major issue. Any timeline to solving this? Thank you

@idotzang I made a tiny package encapsulating this functionality, it may help - https://github.com/saurabh0719/object-tracker

@cr0hn
Copy link
Owner

cr0hn commented Mar 15, 2023

This package seems to do exactly what I need, but this is indeed a major issue.
Any timeline to solving this?
Thank you

Sorry, but currently, I not have so much time to maintain this package, sorry. A PR sounds great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants