Stian Søiland wrote: > On 2005-07-06 16:33:47, Ron Adam wrote: > > >>*No more NamesError exceptions! >> print value >> >> None > > > So you could do lot's of funny things like: > > def my_fun(extra_args=None): > if not extraargs: > print "Behave normally" > extra_args = 1337 > > if extraargs: > asdkjaskdj > .. > if extra_args: > kajsdkasjd
Yes, returning None from an undefined name is DOA. In the above case you would get an error by the way. "if extraargs:" would evaluate to "if None:", which would evaluate to "if:" which would give you an error. >>*No initialization needed for a while loop! >> >> while not something: >> if <condition>: >> something = True > > > This is the only "good" case I could find, but opening for a lots of > errors when you get used to that kind of coding: It would need to be.. while not (something==None): and the compiler would need to handle it as a special case. But this one could still work without allowing something=undefined to be valid. > while not finished: > foo() > finished = calculate_something() > > (..) > (..) # Added another loop > while not finished: > bar() > finished = other_calculation() > > Guess the amount of fun trying to find out the different errors that > could occur when bar() does not run as it should because the previous > "finished" variable changes the logic. It's not really differnt than any other value test we currently use. notfinished = True while notfinished: notfinished = (condition) # Need to set notfinished back to True here. while notfinished: <morestuff> >>*Test if name exists without using a try-except! >> if something == None: >> something = value > > Now this is a question from newcomers on #python each day.. "How do I > check if a variable is set?". > > Why do you want to check if a variable is set at all? If you have so > many places the variable could or could not be set, your program design > is basically flawed and must be refactored. There's a few places the Python library that do exactly that. try: value except: value = something I admit it's something that should be avoided if possible because if there's doubt that a name exists, then there would also be doubt concerning where it came from and weather or not it's value/object is valid. Anyway, it was an interesting but flawed idea, I should of thought more about it before posting it. Cheers, Ron -- http://mail.python.org/mailman/listinfo/python-list