append to non-existing list

Fredrik Lundh fredrik at pythonware.com
Wed Nov 9 09:58:55 EST 2005


Yves Glodt wrote:

> I am fairly new to python (and I like it more and more), but I can not
> answer this question... As I said, where I come from it is possible, and
> how they do it is explained a little here:
> http://lu.php.net/manual/en/language.types.type-juggling.php

but that page doesn't discuss the same thing; it says is that you don't
have to specify the type when you create a variable, which applies to
Python too:

    x = 10 # x is now an integer
    x = "string" # x is now a string

It also says that PHP converts things on the fly when you to operations
that involve different types. Python does that for some types...

    x = 10 + 5.5 # 15.5
    x = "hello" + u"world" # u"helloworld"

....but requires you to spell things out in cases where the types don't have
a "natural" relation:

    x = "10" + 20 # should this be 30 or 1020 or "1020" or ...?

    x = int("10") + 20 # 30
    x = "10" + str(20) # "1020"

In your case, you're using

    x.somemethod(somevalue)

which only provides two clues: you want x to be something that has a
method with a given name, and you're passing in some value.  That's not
enough information to figure out that you want a list and not some other
object that happens to have a method with the same name.

</F>






More information about the Python-list mailing list