3

I am a beginner at python programming and I have written the following program in an attempt to understand scoping and typing rules.

  a = 5                                                                                                                                                                                                 
  print(a)
  a = "Hello World"
  print(a)

I get the following output.

  5
  Hello World

I understand that the variables are dynamically typed in python. Interpreter understands 'a' is an integer when a=5 assignment happens. Why doens't it give an error when the same variable is assigned a string value

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • Similar question with some good answers http://stackoverflow.com/questions/11007627/python-variable-declaration – Scott May 10 '15 at 06:42
  • By the way, this really has nothing to do with scoping rules. You've just got the (module-)global scope here. You only get new scopes by defining functions and classes. (Well, that's not _quite_ true, but close enough for now.) – abarnert May 10 '15 at 06:47
  • It doesn't give an error when the same variable is assigned a string value because Python uses dynamic typing. That's the whole thrust of it. Object types wouldn't be very dynamic if you couldn't change them. – TigerhawkT3 May 10 '15 at 07:00

3 Answers3

9

Variables in Python don't have types. They're just names for values. Those values, of course, do have types, but the variables don't care. Just as "John" can be the name for a man in one conversation or for a poodle in another, a can be the name for an int in one part of your program and a str in another part.


This is part of a much larger difference between variables in Python and whatever language you're probably coming from.

In C++, a variable actually defines a location in memory where values can live. For example, and int variable has 4 bytes of memory, and when you write a = 42, it copies the number 42 into those 4 bytes.

In Python, a variable just defines a reference to a value that has its own life off somewhere on the heap. The value's storage still has to have a type, of course, but the variable isn't the value's storage.

(If C++ really is your most familiar language, it may help to think of every Python variable as being of type std::shared_ptr<boost::any>.)


There are some programs where being able to reuse variable names for different types is useful, but there are others where it's more likely to be a bug than something intentional. There are static analyzers like mypy that you can use to check for such bugs. In most cases, automated type inference is sufficient to figure out what type you wanted a variable to be and make sure you used that type consistently. When it's not, PEP 484 (which will probably be part of Python 3.5, and is already mostly handled by mypy) standardizes a way to help these analyzers by giving them explicit type hints.


One last thing: If you're curious how this works under the covers, in Python, every scope's namespace is just a dictionary. When you write a = 5 at the module-global level, it's just like doing g['a'] = 5, where g is the module's dictionary. There are a few tricky bits with locals and closures, but basically, this is the key to how variables work.

abarnert
  • 354,177
  • 51
  • 601
  • 671
5

Variables do not have types in Python. Objects have types. "Variables" are just names attached to objects. The name a in your example is just a label. You can attach it to any kind of object.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
0

lets take the program line by line and what is happening..

line 1 - you insert a value 5 and assign it to 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 the label "a" now pointed to value "hello world " instead of value 5.

line 4 - print the label "a"

a good way to understand python interpretation is using a visualizer. here is a one good online free app. it gives you the idea of step by step execution of the program. link to visualizer

Nipun Alahakoon
  • 2,772
  • 5
  • 27
  • 45