Re: Replace Whole Object Through Object Method

2006-07-04 Thread digitalorganics
Fredrik Lundh wrote: [EMAIL PROTECTED] wrote: I'm working with a team that's doing social modeling, and for example, I need to model workers that at some point in the program may or may not also become employers. Now, I want the workers to take on all behaviors and attributes of an

Re: Replace Whole Object Through Object Method

2006-07-04 Thread digitalorganics
Point well taken, and indeed a brilliant solution. Thank you I V for demonstrating so clearly. I V wrote: On Mon, 26 Jun 2006 19:40:52 -0700, digitalorganics wrote: A misuse of inheritance eh? Inheritance, like other language features, is merely a tool. I happen to be using this tool to have

Re: Replace Whole Object Through Object Method

2006-07-01 Thread Stefan Schwarzer
Hi Bruno :) On 2006-06-27 10:30, Bruno Desthuilliers wrote: FWIW, a good part of the canonical design patterns are mostly workaround the lack of flexibility in languages like C++ and Java. May be, but that doesn't exclude that some patterns are also useful in Python. :-) The Strategy

Re: Replace Whole Object Through Object Method

2006-07-01 Thread John Roth
[EMAIL PROTECTED] wrote: How can an object replace itself using its own method? See the following code [code deleted] At the risk of creating totally incomprehensible and unmaintainable code, you need two things: First, you can change the class of any new style object by assigning to the

Re: Replace Whole Object Through Object Method

2006-06-27 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: I'm working with a team that's doing social modeling, and for example, I need to model workers that at some point in the program may or may not also become employers. Now, I want the workers to take on all behaviors and attributes of an employer in addition to their

Re: Replace Whole Object Through Object Method

2006-06-27 Thread I V
On Mon, 26 Jun 2006 19:40:52 -0700, digitalorganics wrote: A misuse of inheritance eh? Inheritance, like other language features, is merely a tool. I happen to be using this tool to have my virtual persons change roles at different points in their lifetime, as many real people tend to do.

Re: Replace Whole Object Through Object Method

2006-06-27 Thread Bruno Desthuilliers
Maric Michaud wrote: Le lundi 26 juin 2006 22:37, [EMAIL PROTECTED] a écrit : Won't work because there will be employers that aren't workers. And yes, only some workers will become employers, but also only some employers will also be workers (at some point in program). Let me be more clear:

Re: Replace Whole Object Through Object Method

2006-06-27 Thread Bruno Desthuilliers
I V wrote: On Mon, 26 Jun 2006 19:40:52 -0700, digitalorganics wrote: A misuse of inheritance eh? Inheritance, like other language features, is merely a tool. I happen to be using this tool to have my virtual persons change roles at different points in their lifetime, as many real people tend

Re: Replace Whole Object Through Object Method

2006-06-27 Thread Fredrik Lundh
Bruno Desthuilliers wrote: As a matter of fact, in Python, the class is an attribute of an object. except when it isn't. def add_role(self, role_class): self.roles.append(role_class(self)) And here you create a circular reference between object and roles... and ? Python has a garbage

Re: Replace Whole Object Through Object Method

2006-06-27 Thread Bruno Desthuilliers
Fredrik Lundh wrote: Bruno Desthuilliers wrote: As a matter of fact, in Python, the class is an attribute of an object. except when it isn't. Which are the cases where it isn't ? def add_role(self, role_class): self.roles.append(role_class(self)) And here you create a circular

Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj = NewClass() newobj.__dict__.update(object.__dict__) return newobj def

Re: Replace Whole Object Through Object Method

2006-06-26 Thread Maric Michaud
Le lundi 26 juin 2006 17:57, [EMAIL PROTECTED] a écrit : How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj = NewClass()

Re: Replace Whole Object Through Object Method

2006-06-26 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote: How can an object replace itself using its own method? AFAIK, It can't (but I can be wrong - some guru around ?). See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {})

Re: Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
Maric Michaud wrote: Le lundi 26 juin 2006 17:57, [EMAIL PROTECTED] a écrit : How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {})

Re: Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
Bruno Desthuilliers wrote: [EMAIL PROTECTED] wrote: How can an object replace itself using its own method? AFAIK, It can't (but I can be wrong - some guru around ?). ... FWIW: Python 2.4.3 (#1, Jun 3 2006, 17:26:11) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2

Re: Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
Maric Michaud wrote: ... def MixInto(Class, Mixin): if Mixin not in Class.__bases__: Class.__bases__ += (Mixin,) This doesn't work in most cases (with new style classes), better recreat a type which inherit from Class and Mixin, or Class.__dict__ with Mixin.__dict__. I

Re: Replace Whole Object Through Object Method

2006-06-26 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote: Maric Michaud wrote: (snip) This doesn't work in most cases (with new style classes), better recreat a type which inherit from Class and Mixin, or Class.__dict__ with Mixin.__dict__. What doesn't work exactly? The whole purpose of the mixin is to add

Re: Replace Whole Object Through Object Method

2006-06-26 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : Bruno Desthuilliers wrote: (snip) Instead of exposing problems with your solution, you may want to expose the real use case ? I'm working with a team that's doing social modeling, and for example, I need to model workers that at some point in the program may or

Re: Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
Bruno Desthuilliers wrote: [EMAIL PROTECTED] a écrit : Bruno Desthuilliers wrote: (snip) Instead of exposing problems with your solution, you may want to expose the real use case ? I'm working with a team that's doing social modeling, and for example, I need to model workers that

Re: Replace Whole Object Through Object Method

2006-06-26 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : Bruno Desthuilliers wrote: [EMAIL PROTECTED] a écrit : Bruno Desthuilliers wrote: (snip) Instead of exposing problems with your solution, you may want to expose the real use case ? I'm working with a team that's doing social modeling, and for example, I need to

Re: Replace Whole Object Through Object Method

2006-06-26 Thread Maric Michaud
Le lundi 26 juin 2006 22:37, [EMAIL PROTECTED] a écrit : Won't work because there will be employers that aren't workers. And yes, only some workers will become employers, but also only some employers will also be workers (at some point in program). Let me be more clear: workers -- subset of

Re: Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
Maric Michaud wrote: Le lundi 26 juin 2006 22:37, [EMAIL PROTECTED] a écrit : Won't work because there will be employers that aren't workers. And yes, only some workers will become employers, but also only some employers will also be workers (at some point in program). Let me be more

Re: Replace Whole Object Through Object Method

2006-06-26 Thread digitalorganics
Bruno Desthuilliers wrote: (snip) It is very important that both should maintain attribute values, regardless of whether they take on new roles. Seems obvious. But just a question, BTW: do workers and employers share the same attributes ? And if not, how do you intend to initialize the