KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In class B below I wanted the __set__ function in class A to be called whenever you assign a value to B().a . Instead, setting a value to B().a overwrites B().a with the value. Class C assigning to C().a works correctly, but I wanted to have a separate instance of A for each user class, i.e. I don't want changing 'a' in one instance of C() to change 'a' in all other instances. I wrote a couple of tests to help illustrate the problem. Can you help me define a class that will pass both test1 and test2? class A(object): def __set__(self, instance, value): print "__set__ called: ", value class B(object): def __init__(self): self.a = A() class C(object): a = A() def test1( class_in ): o = class_in() o.a = "test" if isinstance(o.a, A): print "pass" else: print "fail" def test2( class_in ): o1, o2 = class_in(), class_in() if o1.a is o2.a: print "fail" else: print "pass"
Tags (comma-separated)
Save Edits
Cancel