Python suprised me - in a bad way

I talked with someone looking at Ruby to use for a prototype and didn't want Java's verbose syntax and complex APIs. I suggested Python as a good alternative since it got better support for GTK than Ruby and RedHat and others uses the language alot, but it seemed like they had rejected Python as an alternative for such a relative large prototype. I just had to ask why. So he fired up the console on his Mac and typed:


class foo:
def __init__(self, x=[]):
self.y = x

a = foo()

b = foo()


Try it !!!! It wasn't quite what I've expected.

Go figure.....


Anonymous said...

The better way to do this is to declare the class as follows:

class foo:
....def __init__(self, x=None):
........if x:
............self.y = x
............self.y = []

Regardless of the right way to do this, it is not immediately obvious why one shouldn't code a class the first way. An explanation of what happens can be found here: http://www.network-theory.co.uk/docs/pytut/tut_28.html

Anonymous said...

Wow, that's really weird. Not at all what I would have expected. I still like Python, but it definitely has some warts.

Anonymous said...

"the default value is evaluted only once"in function parameters. You are working with the same reference... Tricky !

Buy Levitra said...

Great article! Thanks.

Phentermine said...

Thanks for interesting article.

Anonimous said...

Excellent website. Good work. Very useful. I will bookmark!