Typing rules and regulations in python

I am a beginner in python programming and I wrote the following program to understand the rules of scoping and typing.

  a = 5                                                                                                                                                                                                 
  a = "Hello World"


I am getting the following output.

  Hello World


I understand that variables are dynamically typed in python. The interpreter understands that "a" is an integer when assigned a=5

. Why not give an error when a string value is assigned to the same variable


source to share

3 answers

Variables in Python are typeless. They are just names for values. Of course, these values ​​are of types, but the variables don't care. Just as "John" can be the name of a person in one conversation, or for a poodle in another, it a

can be a name int

in one part of your program and str

another part.

This is part of the much larger difference between variables in Python and whatever language you are likely to come from.

In C ++, a variable actually defines a location in memory where values ​​can live. For example, and a variable int

has 4 bytes of memory, and when you write a = 42

, it copies 42 into those 4 bytes.

In Python, a variable simply defines a reference to a value that has a life of its own somewhere on the heap. Of course, the value store must have a type, but a variable is not a value store.

(If C ++ is indeed your most familiar language, it might help to think of every Python variable as a type std::shared_ptr<boost::any>


There are several programs in which the ability to reuse variable names for different types is useful, but there are others where it is more likely to be a mistake than something intentional. There are static analyzers such as mypy

that you can use to check for errors like this. In most cases, automatic type detection is enough to figure out what type you want the variable to be, and make sure you use that type consistently. When this is not the case, PEP 484 (which will probably be part of Python 3.5 and is mostly being processed already mypy

) standardizes a way to help these parsers by providing explicit type hints.

One last thing: if you're wondering how it works under the covers, in Python, each namespace is just a dictionary. When you write a = 5

at the global module level, it's just like g['a'] = 5

where g

is the module dictionary. There are a few tricky bits with locales and closures, but this is mostly the key to how variables work.



Variables are typeless in Python. Objects are of types. "Variables" are simply names attached to objects. The name a

in your example is just a label. You can attach it to any object.



allows you to execute the program in turn and what happens.

line 1 - you insert the value 5 and assign it to a label called "a"

line 2 - print the label "a"

line 3 - you insert a value called "hello world" and assign it to label "a", and label "a" now points to "hello world" instead of value 5.

line 4 - print the label "a"

A good way to understand python interpretation is to use a renderer. here is one good online app. this gives you an idea of ​​how to step through the program. link to the renderer



All Articles