[Tutor] Determining if an iterator has more elements.

Jeff Shannon jeff@ccvcorp.com
Thu Jul 24 17:01:55 2003


Marc Barry wrote:

> Jeff:
>
> Thanks a million for your answer.  The language is quite easy to learn 
> as I started reading about it on Monday and  I already feel like I 
> have a good grasp on the language.  Since you are knowledgeable on the 
> Python subject, I was wondering if you could offer some input on the 
> following questions: 


Sure, but I'm forwarding this back to the mailing list too, because 
others may have better input than mine.  :)

> 1.  I need a sorted set data type and I noticed that there isn't one 
> in any of the python modules.  I also need just a regular set data 
> type, but I think I solved that by using a dictionary with both the 
> key and the value as the same object.  Do you know if there are any 
> Pyhton libraries that offer a sorted set implementation? 


Nothing standard.  I believe that Sets will become a built-in type in 
Python 2.3 (due out any day now), but I don't know whether they'll be 
sortable.  You could probably work out something that uses a dict to 
emulate a set, and keeps a sort()ed list of keys...  I'm sure I've seen 
discussion about sorted dictionaries before, but can't recall whether it 
was here or on comp.lang.python; a bit of trawling through archives and 
google groups might turn something up.

> 2.  When I made my first Python class I put all of my class member 
> variables directly after the class declaration.  After doing so, I 
> noticed that if I changed the variable in one instance of an object it 
> was changed in all instances (i.e. static variables).  So I moved all 
> the variables inside the __init__ method and then everything seemed to 
> act like I expected (i.e. as instance variables).  So is the previous 
> how you define static variables in Pyhton and the latter how you 
> define instance variables? 


Yes, pretty much, except that in Python they're typically called class 
variables rather than static variables.  Also, you can get some 
potentially confusing effects depending on whether you're modifying an 
object or rebinding a name.  Here's a short example:

 >>> class breakfast:
...     spam = 0
...     food = ['eggs']
...    
 >>> s1 = breakfast()
 >>> s2 = breakfast()
 >>> s1.spam
0
 >>> s1.spam = 3
 >>> s1.spam
3
 >>> s2.spam
0
 >>>

At first, both s1 and s2 share the class variable 'spam', which is 0. 
 However, when I rebind the name by doing 's1.spam = 3', I'm creating a 
*new* instance variable named 'spam' on instance s1.  s2, however, is 
still using the class variable.

 >>> s1.food
['eggs']
 >>> s1.food.append('sausage')
 >>> s2.food
['eggs', 'sausage']
 >>> s1.food
['eggs', 'sausage']
 >>>

Here, the change made to one showed up in both.  What's the difference? 
 Here, I've taken an existing object  (the list pointed to by 
breakfast.food) and mutated it by adding another element.  However, I 
haven't changed any of the names binding that list to my class (or 
either instance).  Since I haven't created a new instance variable for 
s1, it's still pointing to the class variable, as is s2.  

The concept of name binding and object mutation is very important in 
Python, so if this is at all confusing to you, it might be good to 
search out some more thorough descriptions from the online tutorial and 
the list/newsgroup archives.

> Also, I wasn't sure if I should reply to you via the mailing list.  I 
> am not sure of the protocol and so please inform me if I shouldn't 
> contact you directly. 


I don't mind being contacted directly, but it's usually best to at least 
send a copy of things like this to the list.  That way, if I should 
happen to be busy, someone else has the chance to answer your 
questions... and if I happen to give a wrong answer, someone else can 
correct me.  :)

Jeff Shannon
Technician/Programmer
Credit International