direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
What's the difference between initializing class variables within the class definition directly versus initializing them within the class's __init__ method? Is there a reason, perhaps in certain situations, to choose one over the other? Thank you. -- http://mail.python.org/mailman/listinfo/pytho

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Ah, you've brought me much clarity Diez, thank you. That would explain some "bugs" I've been having... Diez B. Roggisch wrote: > [EMAIL PROTECTED] schrieb: > > What's the difference between initializing class variables within the > > class definition directly versus initializing them within the c

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Wait a minute! It doesn't explain my bugs. I've got "class variables" acting like instance variables. What's weirder is that this behavior occurs on my computer (in both of my installed WinXP copies) but not on my laptop (WinXP Pro). See the following test: class Boo: jerk = "yes" def ki

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > > > At first, I thought that self.jerk was resolving to the class attribute > > instead of creating a new variable (w/ a differing scope). > > When you access an instance attribute, Python first looks in the > instance object, and then in the class

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Thank you Dennis, this makes the behavior so much clearer to me. I see now that when self.jerk = self.jerk + 1 is executed that even though the names are identical, at this point I'm referring to two different values (one which is being created in part from the other). As for my laptop, I'm not r

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > > > Any clue what's behind this behavior? > > a missing plus sign. > > Thanks for the guess but not possible given the following: class Boo: jerk = 10 def killjerk(self): counter = 3 while counter !=0: counte

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Oh wow, I wasn't expecting so much help. I really appreciate it. My problem, however, has been solved. I uninstalled my ActiveState Python distro on my laptop and installed the distro from python.org along with Stan's Python Editor. I ran the same code I'd run before and guess what? The behavior no

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > > > Output from laptop comp.: > > > > 1 > > 10 > > 2 > > 10 > > 3 > > 10 > > so how are you entering and running the code on your laptop ? > > what happens if you set the class attribute to 100 instead of 10 ? > > You can see my other post which

Re: direct initialization of class attributes vs. declarations w/in __init__

2006-06-11 Thread digitalorganics
Diez B. Roggisch wrote: > > No need to be obnoxious. I do appreciate your efforts to help, but you > > must admit, your last statement is a bit snide and certainly not > > useful. > > I'm telling you that the code runs differently on my laptop. > > It certainly doesn't. There is absolutely no ima

Re: Very nice python IDE (windows only)

2006-06-12 Thread digitalorganics
Great IDE! I love it. Two things that make me very happy: 1. Unlike PythonWin and Stan's Python Editor (SPE), PyScripter shows not just methods but also attributes in the class browser. [I'll mention that Eric3 also does this, but I don't use Eric3 much because the editor component doesn't allow s

Re: Very nice python IDE (windows only)

2006-06-12 Thread digitalorganics
Jan Bijsterbosch wrote: > Hello ago, Bernard, > > "ago" <[EMAIL PROTECTED]> schreef in bericht > news:[EMAIL PROTECTED] > > > > Bernard Lebel wrote: > >> Not me. I'll probably sound pedantic but > >> - the editor text looks awful, changing the editor options had no effect > >> at all > >> - there

Re: Very nice python IDE (windows only)

2006-06-12 Thread digitalorganics
By the way, does anyone know if / how you can change the key bindings in PythonWin? In PyScripter, I was quite pleased that the autocomplete lets you make your selection by pressing enter (natural), yet in PythonWin you have to press tab (unnatural). Thanks. [EMAIL PROTECTED] wrote: > Great IDE! I

Bridge: Ruby to Python communication

2006-06-12 Thread digitalorganics
Hello all. I want a ruby and a python module to be able to communicate with each other, access classes, instances and the like. Is there a bridge for this? I'm aware of rupy, but the documentation seems rather inadequate for the uninitiated. Are there other libraries/bridges or maybe a rupy tutoria

Re: Bridge: Ruby to Python communication

2006-06-12 Thread digitalorganics
I'll check that out, thanks! vasudevram wrote: > [EMAIL PROTECTED] wrote: > > Hello all. I want a ruby and a python module to be able to communicate > > with each other, access classes, instances and the like. Is there a > > bridge for this? I'm aware of rupy, but the documentation seems rather >

Re: Bridge: Ruby to Python communication

2006-06-12 Thread digitalorganics
Wait wait, what do I do exactly? Thanks Michel. Michel Claveau wrote: > Hi! > > For me, like PHP (message of 11h.35) : > > Only in Windows, I can call Ruby (more exactly Ruby-script) from > Python. > I can, also, call, from Python, Ruby-defined-functions, like a method > of a Python-class. > > It'

Mix-In Class Methods At Run-Time

2006-06-24 Thread digitalorganics
Hi all. If I have an instance of class A, called say foo, and I need to mix-in the functions and variables of another class (class B) to this instance at runtime, how do I do it? In other words, I want to make foo an instance of an anonymous and temporary class that inherits its functionality from

Re: Mix-In Class Methods At Run-Time

2006-06-25 Thread digitalorganics
This looks excellent bearophile, but I'm having trouble understanding some things. Perhaps you can help wipe clean my ignorance. Firstly, I thought __classes__ was a read-only attribute? Secondly, what is a "dictproxy object" and why won't the following code work: class Cat: def meow(self):

Re: Mix-In Class Methods At Run-Time

2006-06-25 Thread digitalorganics
Okay, while I'd still like to know the answer(s) to my earlier question(s), I've mostly solved my problem thanks to bearophile and my own learning. An example: class Cat(object): def __init__(self): self.love = 0 def meow(self): print "meow" class Dog(object): def bark(

Re: Mix-In Class Methods At Run-Time

2006-06-26 Thread digitalorganics
Bruno Desthuilliers wrote: > [EMAIL PROTECTED] wrote: > (snip) > > > Two additional questions though: 1) is there a way for a function to > > get a reference to its caller automatically (as in, without the caller > > having to pass it in)? > > It's possible with sys._getframe() and a decorator - b

DictProxy? What is this?

2006-06-26 Thread digitalorganics
When I tried to update a class's __dict__, I got an error saying that there is no 'update' attribute for dictproxy object. What is a dictproxy object? I thought that __dict__ is always suppose to be a, no kidding, dictionary? Hence there should indeed be an update method. What's this proxy business

Re: DictProxy? What is this?

2006-06-26 Thread digitalorganics
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > > > When I tried to update a class's __dict__, I got an error saying that > > there is no 'update' attribute for dictproxy object. What is a > > dictproxy object? > > a CPython implementation detail, used to protect an internal data structure > us

Re: DictProxy? What is this?

2006-06-26 Thread digitalorganics
All right. Thanks for explaining that Maric. Maric Michaud wrote: > Le lundi 26 juin 2006 16:06, [EMAIL PROTECTED] a écrit : > > Fredrik Lundh wrote: > > > [EMAIL PROTECTED] wrote: > > > > When I tried to update a class's __dict__, I got an error saying that > > > > there is no 'update' attribute

Get List of Classes

2006-06-26 Thread digitalorganics
Is there a method or attribute I can use to get a list of classes defined or in-use within my python program? I tried using pyclbr and readmodule but for reason that is dogslow. Thanks in advance DigiO -- http://mail.python.org/mailman/listinfo/python-list

Search String for Word

2006-06-26 Thread digitalorganics
What's the best way to search a string for a particular word and get a booleen value indicating whether it exists in the string or not? Thanks... -- http://mail.python.org/mailman/listinfo/python-list

Re: Search String for Word

2006-06-26 Thread digitalorganics
I thought I'd seen that somewhere! Thanks Tim. I was previously using re.search(substring, targetstring). Tim Chase wrote: > > What's the best way to search a string for a particular word and get a > > booleen value indicating whether it exists in the string or not? > > >>> substring = 'foo' > >

Re: Search String for Word

2006-06-26 Thread digitalorganics
And what if I want to search for an item in a tuple, is there a similarly easy method? Tim Chase wrote: > > What's the best way to search a string for a particular word and get a > > booleen value indicating whether it exists in the string or not? > > >>> substring = 'foo' > >>> targetstring = '

Re: Get List of Classes

2006-06-26 Thread digitalorganics
Wow, more than I had asked for, thank you Tim! I ended up doing this: def isClass(object): if 'classobj' in str(type(object)): return 1 elif "'type'" in str(type(object)): return 1 else: return 0 def listClasses(): classes = [] for eachobj in globals().

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 is

Re: Search String for Word

2006-06-26 Thread digitalorganics
Thank you thank you! Tim Williams wrote: > On 26 Jun 2006 08:24:54 -0700, [EMAIL PROTECTED] > <[EMAIL PROTECTED]> wrote: > > And what if I want to search for an item in a tuple, is there a > > similarly easy method? > > > > Tim Chase wrote: > > > > What's the best way to search a string for a part

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)] o

Re: Mix-In Class Methods At Run-Time

2006-06-26 Thread digitalorganics
Bruno Desthuilliers wrote: > [EMAIL PROTECTED] wrote: > > Bruno Desthuilliers wrote: > > > >>[EMAIL PROTECTED] wrote: > (snip) > > >>>and 2) what's the reason to use newstyle classes > >>>versus the old? > >> > >>All this is explained on python.org (there's a menu entry for this in > >>the documen

TypeError: Cannot create a consistent method resolution order (MRO) for bases object

2006-06-26 Thread digitalorganics
What are the reason one would get this error: TypeError: Cannot create a consistent method resolution order (MRO) for bases object ?? I can provide the code if needed -- http://mail.python.org/mailman/listinfo/python-list

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__

Re: TypeError: Cannot create a consistent method resolution order (MRO) for bases object

2006-06-26 Thread digitalorganics
Hint: If I change Dog and Bat to old-style classes, there's no problem, everything works fine. Okay, here's the code dump from my playground:: -- #!/usr/bin/env python class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object._

Re: TypeError: Cannot create a consistent method resolution order (MRO)for bases object

2006-06-26 Thread digitalorganics
Oh, I forgot the line that bombed, sorry: line 52, in __main__ Bat.__bases__ += (Dog,) TypeError: Cannot create a consistent method resolution order (MRO) for bases object, Mixin, Dog See my other post for the complete code and my relevant note about new-style classes, thanks... Terry Reedy

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

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 > >

Re: TypeError: Cannot create a consistent method resolution order (MRO) for bases object

2006-06-26 Thread digitalorganics
Maric Michaud wrote: > Le lundi 26 juin 2006 20:06, [EMAIL PROTECTED] a écrit : > > What are the reason one would get this error: TypeError: Cannot create > > a consistent method resolution order (MRO) for bases object ?? > > > > I can provide the code if needed > This is the python solution t

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 initia

Traversing Inheritance Model

2006-06-26 Thread digitalorganics
What's the best way to traverse the web of inheritance? I want to take a class and traverse its bases and then the bases' bases etc looking for a particular class. What first came to mind was nested for loops. However, I want to know if there's some pre-existing method for doing this or if this

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 o

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 usi