Python help
Paul Lussier
p.lussier at comcast.net
Thu Feb 5 13:21:58 EST 2004
In a message dated: Thu, 05 Feb 2004 11:17:29 EST
Cole Tuininga said:
>First of all, welcome to python! 8) I do a fair amount of python
>coding and am always happy to help out if I can.
Thanks, I've heard so much about it, I figured it couldn't hurt
to add tool to my tool-box :)
>Quick question - what version of python are you running?
2.2 I think.
>Keep in mind that instantiation of objects is one of python's slower
>operations. If speed matters, you'd do much better to use a dictionary.
In this case, speed doesn't matter, but that's good to know, as I'll
eventually be doing some web/CGI programming where it may well matter
(is python good for web/CGI ?)
>> group.name = line[0] # what I don't quite get is *why* this works
>> group.gid = line[2] # but it certainly beats the dealing
>> # with nested/anon dictionaries
>> # (though I think I like
>> # perl's hashes better
>>
>> group.members = line[3].split(',')
>> gr_hash = {group.name: group}
>
>Why this works is that unless you do some work to prevent it, objects
>will create member variable references on the fly. You can't do a
>test against on e before you set it, so for instance, doing something
>like: if group.bob == "something":
>would throw an exception, unless you explicit define group.bob before
>hand.
Ahh, okay. That's good to know. I can see where I might end up with
an empty gun and foot full lead doing this :)
>You're essentially using an instance of an object as a dict,
>which again leads me to think you should just use a dict. 8)
Well, my thinking was that it was more a long the lines of a
'struct' in C, as noted in the python tutorial.
>Note, the code as I'm reading it right now will not work. You will end
>up with a single entry repeated many times in the output. The reason
>for this is that you are using the same object over and over. Keep in
>mind that python is heavily referential. When you say gr_hash =
>{group.name: group} but not changing the reference to group, each of the
>items are going to point to the same object.
Ahh, I had assumed that by changing the object member values,
I'd get those. But I see your point now. Of course, with my one line
test file, this wasn't an issue :)
>What I *think* you want is to put the "group = gr_struct()" line within
>the loop.
I tried that, but it too seemed to exhibit the above described behavior.
(of course I didn't look too closely at it in other than to just run the
script. At some point when I have more time, I'll play with it in the
debugger).
>> members = gr_hash[key].members # why can't I do
>> # gr_hash[key].members.sort()
>> members.sort()
>
>The reason you can't do this in place is that sort does not return the
>sorted list - it sorts it in place. I believe the return value for the
>sort() method is a None object. One neat thing about sort is that you
>can pass it your own comparison function.
Hmmm, okay. That's similar to perl's sort { func } list thingy.
>string is not a great name for a variable, IMNSHO. 8)
It's better than 'str' which is what I started with ;)
I know. I'm just messing around right now, and will clean this all up
a great deal when I figure out what I'm doing.
>I believe the join syntax you want is:
>member_names = ','.join( members )
Interesting. I didn't know you could do that!
Thanks,
Seeya,
Paul
--
Key fingerprint = 1660 FECC 5D21 D286 F853 E808 BB07 9239 53F1 28EE
It may look like I'm just sitting here doing nothing,
but I'm really actively waiting for all my problems to go away.
If you're not having fun, you're not doing it right!
More information about the gnhlug-discuss
mailing list