Jump to content

Python 's system of types and variables is different from many other languages, but it 's impor...


G+_Lee Crocker
 Share

Recommended Posts

Python's system of types and variables is different from many other languages, but it's important that people learn it correctly, so I'll try to simplify as much as I can:

 

First thing: variables in Python are names of things. Things--that is, objects, numbers, strings, and such--have a type, but names of things don't. Variables DO NOT have types. The bears repeating, because Padre glossed over it quickly, leaving a wrong impression. When you run this code:

 

    var1 = 6

    var2 = "Fred"

 

two objects are created: an integer and a string. Also, two names are created, and they are made to point to those things. But the variables are not assigned types based on their value. They are just names, they have no type, and they are perfectly free to point to other things. So, for example,

 

    print type(var1)

 

will print

 

   

 

because it currently points to an integer, BUT, 

 

    var1 = "Barney"

 

is a perfectly legal assignment, after which,

 

    print type(var1)

 

will print

 

   

 

because now it points to a string. What happened to the integer object "6"? It no longer has any names pointing to it, so the memory it took is marked for recycling. Python handles memory management automatically--if an object is still in use somewhere, it stays in memory. When nobody's using it anymore, its memory is reclaimed.

 

In a language with typed variables, a variable points to a specific piece of memory, and that can only contain one type of thing. But in Python, the name "var1" is just a name--it can point to an integer, a string, a list, an object, whatever. The thing itself has a type, and a location in memory, the name doesn't. In fact, if you did:

 

    var3 = var1

 

You would now have another name pointing to the same value that var1 points to, but it's NOT another string with the same value--it's the same string, at the same location in memory. There's only one object (the string "Barney"), but two different names pointing to it. This distinction becomes critically important when the things are objects and lists and such, because changing an object via one name will cause that change to appear when accessed from another name.

 

I know this sounds like a fine distinction, but it really it critical to learning Python.

Link to comment
Share on other sites

Yes, all variables are just references, as are list elements and hash keys and values. Objects are allocated and freed dynamically by reference counting: when no variable or list or hash table points to an object, it is freed. And this is probably way beyond today's lesson, but "primitives" like numbers and strings and functions are immutable objects in Python.

Link to comment
Share on other sites

Names do have scope, just as in other languages. For example, if you have a function like this:

 

    def fn(a, b):

        a = 1

        return a+b

 

And code like this:

 

    a = 5

    print fn(a, 2)

    print a

 

The code will print "3", and then "5". The name "a" outside the function is different from the name "a" inside the function. Likewise, things like object members are in their own namespace. If you need to access a global name from a scope in which there is a local one, you can explicitly declare that you want the global.  But as with any language, globals are evil and there's no real reason to ever use them--except maybe the first day of lessons. The idea of namespaces in general is another important one in Python with a lot of uses.

Link to comment
Share on other sites

 Share

×
×
  • Create New...