Re: Merging Objects

2006-04-03 Thread Michael Spencer
Cloudthunder wrote:
> Sorry, I don't understand, how does this solve my problem?
> 
__getattr__ and __setattr__ allow you to set up dynamic delegation e.g.,

class Foo(object):
 def __init__(self, **kw):
 self.__dict__.update(kw)
 def methFoo(self, x):
 return "Foo.methFoo(%s,%s)" % (self,x)

class Bar(object):
 def __init__(self, **kw):
 self.__dict__.update(kw)
 def methBar(self, x):
 return "Foo.methFoo(%s,%s)" % (self,x)

class MultiDelegate(object):
 def __init__(self, *args):
 object.__setattr__(self,"_objects",args)
 def __getattr__(self, attr):
 for obj in self._objects:
 if attr in dir(obj):
 return getattr(obj, attr)
 types = ",".join(obj.__class__.__name__ for obj in self._objects)
 raise AttributeError, "%s object has no attribute '%s'" % (types, attr)
 def __setattr__(self, attr, value):
 # but you could do something more useful here too
 raise TypeError, "Can't set attributes of MultiDelegate"

 >>> f = Foo(a=1)
 >>> b = Bar(b=2)
 >>> m = MultiDelegate(f,b)
 >>> m.a
1
 >>> m.b
2
 >>> m.methFoo(1)
'Foo.methFoo(,1)'
 >>> m.methBar(1)
'Foo.methFoo(,1)'
 >>>

HTH
Michael

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Merging Objects

2006-04-03 Thread Cloudthunder
Sorry, I don't understand, how does this solve my problem?On 4/3/06, Jesus Rivero - (Neurogeek) <[EMAIL PROTECTED]
> wrote:-BEGIN PGP SIGNED MESSAGE-Hash: SHA1>>> help(getattr) help(setattr)
Regards,  Jesus Rivero - (Neurogeek)Cloudthunder wrote:> Question: how do I merge two objects? I would like to be able to> have an instance of Foo and an instance of Boo and then be able to
> add them together and create a new object that has the methods and> properties of both objects. I am going to be doing a lot of this> and I don't want to have to dynamically (or otherwise) create a
> whole bunch of classes that inherit from both Foo and Boo then> create instances of those new children classes. In fact, this> wouldn't work for me anyway because I am going to have an instance> of Foo which will be used within my algorithm for sometime then
> later I will want to merge it with a fresh instance of Boo or> otherwise give it all the methods and properties of the Boo class.> Am I making any sense here?>> Thanks.>> - OP
>> P.S. I also noticed that we can no long use the __members__> property to get a tuple of all a class's methods. How can I get the> same info from dir()? Dir(), I understand, tells you about all
> properties and you can't tell it to just list the methods.-BEGIN PGP SIGNATURE-Version: GnuPG v1.4.1 (GNU/Linux)Comment: Using GnuPG with Thunderbird - 
http://enigmail.mozdev.orgiD8DBQFEMWPzdIssYB9vBoMRAnkLAJ4mkUTlB9POlOyE5MeHukAJ5LeawQCghtoQDsn33bw0LYFsNS2AYStPLdU==Dt4g-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Merging Objects

2006-04-03 Thread Jesus Rivero - (Neurogeek)
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

>>> help(getattr) help(setattr)

Regards,

  Jesus Rivero - (Neurogeek)

Cloudthunder wrote:

> Question: how do I merge two objects? I would like to be able to
> have an instance of Foo and an instance of Boo and then be able to
> add them together and create a new object that has the methods and
> properties of both objects. I am going to be doing a lot of this
> and I don't want to have to dynamically (or otherwise) create a
> whole bunch of classes that inherit from both Foo and Boo then
> create instances of those new children classes. In fact, this
> wouldn't work for me anyway because I am going to have an instance
> of Foo which will be used within my algorithm for sometime then
> later I will want to merge it with a fresh instance of Boo or
> otherwise give it all the methods and properties of the Boo class.
> Am I making any sense here?
>
> Thanks.
>
> - OP
>
> P.S. I also noticed that we can no long use the __members__
> property to get a tuple of all a class's methods. How can I get the
> same info from dir()? Dir(), I understand, tells you about all
> properties and you can't tell it to just list the methods.


-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEMWPzdIssYB9vBoMRAnkLAJ4mkUTlB9POlOyE5MeHukAJ5LeawQCghtoQ
Dsn33bw0LYFsNS2AYStPLdU=
=Dt4g
-END PGP SIGNATURE-

-- 
http://mail.python.org/mailman/listinfo/python-list


Merging Objects

2006-04-03 Thread Cloudthunder
Question: how do I merge two objects? I would like to be able to have an instance of Foo and an instance of Boo and then be able to add them together and create a new object that has the methods and properties of both objects. I am going to be doing a lot of this and I don't want to have to dynamically (or otherwise) create a whole bunch of classes that inherit from both Foo and Boo then create instances of those new children classes. In fact, this wouldn't work for me anyway because I am going to have an instance of Foo which will be used within my algorithm for sometime then later I will want to merge it with a fresh instance of Boo or otherwise give it all the methods and properties of the Boo class. Am I making any sense here? 
Thanks. - OPP.S. I also noticed that we can no long use the __members__ property to get a tuple of all a class's methods. How can I get the same info from dir()? Dir(), I understand, tells you about all properties and you can't tell it to just list the methods.

-- 
http://mail.python.org/mailman/listinfo/python-list