Re: Assigning to self.__class__

2006-01-26 Thread Terry Reedy
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >I have some places in pyparsing where I've found that the most > straightforward way to adjust an instance's behavior is to change its > class. > I do this by assigning to se

Re: Assigning to self.__class__

2006-01-26 Thread Alex Martelli
Heiko Wundram <[EMAIL PROTECTED]> wrote: > bruno at modulix wrote: > > Paul McGuire wrote: > >> or am I taking advantage of a fortuitous accident, which may get > >> undone at a future time? > > > > It's certainly not a fortuitous accident. > > And even the (printed) cookbook has examples which

Re: Assigning to self.__class__

2006-01-26 Thread Heiko Wundram
bruno at modulix wrote: > Paul McGuire wrote: >> or am I taking advantage of a fortuitous accident, which may get >> undone at a future time? > > It's certainly not a fortuitous accident. And even the (printed) cookbook has examples which assign to self.__class__... I guess this means this featu

Re: Assigning to self.__class__

2006-01-26 Thread bruno at modulix
ython is to : >(snip) assigning to self.__class__, (snip) !-) > > Any comments on this practice? It can be very confusing for newbies and peoples having no experience with *dynamic* languages, and I guess control-freaks and static-typing-addicts would runaway screaming. But I like it

Assigning to self.__class__

2006-01-26 Thread Paul McGuire
I have some places in pyparsing where I've found that the most straightforward way to adjust an instance's behavior is to change its class. I do this by assigning to self.__class__, and things all work fine. (Converting to use of __new__ is not an option - in one case, the change is

Re: Assigning to self

2005-02-12 Thread top
Jeff Shannon wrote: > class __Foo: > "I am a singleton!" > pass > > def Foo(foo_obj = __Foo()): > assert isinstance(foo_obj, __Foo > return foo_obj this is a bit simpler, I think, and takes advantage from Python's free name-rebinding. class Singleton(object): def __call__(s

Re: Assigning to self

2005-01-18 Thread Jeff Shannon
Marc 'BlackJack' Rintsch wrote: Frans Englich wrote: Then I have some vague, general questions which perhaps someone can reason from: what is then the preferred methods for solving problems which requires Singletons? As already mentioned it's similar to a global variable. If I need a "Singleton"

Re: Assigning to self

2005-01-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Frans Englich wrote: > Then I have some vague, general questions which perhaps someone can reason > from: what is then the preferred methods for solving problems which requires > Singletons? As already mentioned it's similar to a global variable. If I need a "Singleton"

Re: Assigning to self

2005-01-17 Thread John Roth
"Frans Englich" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] On Monday 17 January 2005 20:03, John Roth wrote: "Frans Englich" <[EMAIL PROTECTED]> wrote in message In other words, you're trying to create a singleton. In general, singletons are frowned on these days for a number of

Re: Assigning to self

2005-01-17 Thread Frans Englich
On Monday 17 January 2005 21:24, Peter Otten wrote: > Frans Englich wrote: > > On Monday 17 January 2005 20:03, John Roth wrote: > >> "Frans Englich" <[EMAIL PROTECTED]> wrote in message > > > > > > > >> In other words, you're trying to create a singleton. In general, > >> singletons are frowned o

Re: Assigning to self

2005-01-17 Thread Peter Otten
Frans Englich wrote: > On Monday 17 January 2005 20:03, John Roth wrote: >> "Frans Englich" <[EMAIL PROTECTED]> wrote in message > > > >> In other words, you're trying to create a singleton. In general, >> singletons are frowned on these days for a number of reasons, >> not least because of the

Re: Assigning to self

2005-01-17 Thread Peter Otten
Frans Englich wrote: >> > >>> class Foo(object): >> > >> > ... cache = {} >> > ... def __new__(cls, id): >> > ... try: >> > ... return cls.cache[id] >> > ... except KeyError: >> > ... pass >> > ... cls.cache[id] =

Re: Assigning to self

2005-01-17 Thread Frans Englich
On Monday 17 January 2005 20:03, John Roth wrote: > "Frans Englich" <[EMAIL PROTECTED]> wrote in message > In other words, you're trying to create a singleton. In general, > singletons are frowned on these days for a number of reasons, > not least because of the difficulty of testing them. Then

Re: Assigning to self

2005-01-17 Thread Frans Englich
On Monday 17 January 2005 20:55, Frans Englich wrote: > On Monday 17 January 2005 19:02, Peter Otten wrote: > > Frans Englich wrote: > > > What the code attempts to do is implementing a, to the API user, > > > transparent memory-saver by ensuring that no more than one instance of > > > the class fo

Re: Assigning to self

2005-01-17 Thread Frans Englich
On Monday 17 January 2005 19:02, Peter Otten wrote: > Frans Englich wrote: > > What the code attempts to do is implementing a, to the API user, > > transparent memory-saver by ensuring that no more than one instance of > > the class foo exists for a particular id. E.g, the user can simply > > "crea

Re: Assigning to self

2005-01-17 Thread John Roth
"Frans Englich" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Hello, [...] What the code attempts to do is implementing a, to the API user, transparent memory-saver by ensuring that no more than one instance of the class foo exists for a particular id. E.g, the user can simply "crea

Re: Assigning to self

2005-01-17 Thread Paul McGuire
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > An implementation of what you want can be found here: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558 > I think this recipe pre-dates the introduction of __new__ in Python. How about something like this (requires the nam

Re: Assigning to self

2005-01-17 Thread Reinhold Birkenfeld
Frans Englich wrote: > Hello, > > I am having trouble with throwing class instances around. Perhaps I'm > approaching my goals with the wrong solution, but here's nevertheless a > stripped down example which demonstrates my scenario: > > #

Re: Assigning to self

2005-01-17 Thread Peter Otten
Frans Englich wrote: > What the code attempts to do is implementing a, to the API user, > transparent memory-saver by ensuring that no more than one instance of the > class foo exists for a particular id. E.g, the user can simply "create" an > instance and if one not already exists, it is created.

Re: Assigning to self

2005-01-17 Thread Mark McEahern
Frans Englich wrote: Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my scenario: [snip] The basic problem seems to be that you're trying to avoid creat

Re: Assigning to self

2005-01-17 Thread [EMAIL PROTECTED]
An implementation of what you want can be found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558 -- http://mail.python.org/mailman/listinfo/python-list

Re: Assigning to self

2005-01-17 Thread Frans Englich
On Monday 17 January 2005 18:45, Frans Englich wrote: > The line 'self = me'(scary..) doesn't really > work for the attribute attr; the attribute exists on line 21, but it fails > when yo() tries to access it. Typo; line 21 is yo(). I ment line 18, the print statement in __init__. Cheers,

Assigning to self

2005-01-17 Thread Frans Englich
Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my scenario: #--