Acquisition Tutorial
Once upon a time I was pulling my hair out trying to figure out how acquisition really worled. This is some stuff I came up with.
If we define:
class A: def foo(self): print self
class B: pass
a = A() b = B()
If we now try: A.foo(a) we get <__main__.A instance at 80cb8a8> But if we try: A.foo(b) we get A.foo(b) TypeError: unbound method must be called with class instance 1st argument
This is because the class method foo in A can only be called with a self arguement of an instance of A, not an instance of B.
So if we define:
import Acquisition class A(Acquisition.Implicit): def foo(self): print self
class B(Acquisition.Implicit): pass
a = A() b = B()
- b = B()
This is because we are essentially trying A.foo(a.b) which is equivilant to A.foo(b) which wont work, so the acquisition wrapper tries A.foo(a) instead, which is successfull.
If a and b were the same class type, the acquisition wrapper would do as we expect, like defining:
import Acquisition
class A(Acquisition.Implicit): def foo(self): print self.bar
a = A() a.bar = "a" a.b = A() a.b.bar = "b"
then trying: a.foo() a.b.foo() would result in: a b
If we want to aquire a method from a different class type and have the instance of the calling class passed to it, we need to rely on inheritance rather than the acquisition wrapper(note...we don't even need to use the acquisition wrapper). We do this by making all our classes inherit from a base class that has the particular method:
class C: def foo(self): print self
class A(C): pass
class B(C): pass
a = A() b = B() a.b = B()
So if we try: a.b.foo() a.foo() b.foo() we get: <__main__.B instance at 80bfb18> <__main__.A instance at 80a2be8> <__main__.B instance at 80bfae0> which is the behaviour we want. If we wanted foo() to behave differenty depending on which class is calling it, we could test for the existence or value of a particular attribute:
class C: def foo(self): print self.bar
class A(C): bar = "a"
class B(C): bar = "b"
a = A() b = B() a.b = B()
If we try: a.b.foo() a.foo() b.foo() we get: b a b