1

I'm curious why built-in data types like int, dict, and str aren't keywords. For example, I can use int as a variable and assign it to str. And then when the int object is evaluated, it acts like str. Only in Python 3 have Booleans now become keywords.

When I told my instructor that built-in data types aren't keywords, she said it's hideous form and that we should assume that they are keywords. Is it simply a quirk of Python?

Can anybody shed some light on that?

Edit: Given that I can't add comments, let me be a little more specific as to what I meant as 'keywords'. I mean the identifiers in keyword.kwlist. After I posted, I found Why were True and False changed to keywords in Python 3 which gives some reasons as to why True and False are now keywords. Thanks for answers to my question.

Community
  • 1
  • 1
bilio
  • 31
  • 3
  • Why should they be keywords? – Ignacio Vazquez-Abrams Mar 27 '14 at 05:07
  • 1
    I think the intention of your instructor was to teach you to avoid shadowing of built-ins in your code. – Burhan Khalid Mar 27 '14 at 05:17
  • there are no keywords in python as such, just statements. `int` is not a statement it is a name that happens (by design) to be bound by default to the int type. All names can be rebound to other objects. – Tim Hoffman Mar 27 '14 at 05:19
  • @TimHoffman If there are no keywords, then what does `import keyword; keyword.kwlist` contain? – Hyperboreus Mar 27 '14 at 05:24
  • @hyperboreus `import` is a statement, keyword in this sentence means names of modules etc... keyword is just a name. There are no reserved "keywords" only statements as I understand the original question meant. – Tim Hoffman Mar 27 '14 at 05:28
  • @TimHoffman I was actually referring to the content of `keyword.kwlist`. That is `['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']`. E.g. `or` is not a statement. – Hyperboreus Mar 27 '14 at 05:30
  • Actually that is a list of types `None`, statements `raise` and operators `or` and yes they are "keywords" but there is no keyword thing in python. So I do agree with you there are keywords in python as per the docs which say "The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers." However some of these keywords for instance `False` and `True` can be rebound to something else. ie `False=10` is valid python but not recommended. – Tim Hoffman Mar 27 '14 at 05:43
  • @TimHoffman fortunately in Python3 if you try to assign to `False` you get `SyntaxError: assignment to keyword`. – m.wasowski Mar 27 '14 at 06:02
  • And a good thing too. False = True is somewhat odd to behold :-) – Tim Hoffman Mar 27 '14 at 06:07

3 Answers3

1

Python is dynamically typed and strongly typed, which means you don't need to declare types - they are deducted when you bind object to name. But types are always preserved and do not change until you explicitly rebind new object of different type to name ('variable').

What you call 'keywords' are only built-in utility functions, that return an object, so int() returns default integer ('0') and str() returns empty string. Thus if you call myvar = int() you python first creates new integer object and later binds this object to name myvar in current name scope. Later if you call print myvar it checks current scope for name myvar and passes object this name references to print.

Now, if you for some reason want to make a variable with name 'int', let's say int = str, Python allows that. But it does not mean you changed what name int was referencing to (btw, Python does not allow modifying built-in types), you are only rebinding name you use in your program to other object. Thus if you later call othervar = int() you create new empty string bound to othervar, but that will be only in your current scope, it won't change how other modules can use int(), as they have their own name bindings.

Look at code below to see how it works:

def int_fn():
    return int()

def float_fn():
    int = float
    return int()

Now look what is returned:

In [26]: int()
Out[26]: 0

In [27]: float()
Out[27]: 0.0

In [28]: int_fn()
Out[28]: 0

In [29]: float_fn()
Out[29]: 0.0

Now about what your instructor said. int() or str() indeed are not keywords, and example above show that they are names like any other. But you should treat them as 'reserved words' that you should avoid using as names, unless you really, really know what you are doing and what consequences of your actions are (and that might be hard to learn).

m.wasowski
  • 6,329
  • 1
  • 23
  • 30
0

I'd guess it's because python is dynamically typed so it doesn't need to reserve keywords for each of the different types.

lightandlight
  • 1,345
  • 3
  • 10
  • 24
0

Because Python is a Dynamically typed Language. There are many other languages other then python which are dynamically typed. Very nice discussion about pros and cons of Static Typing and Dynamic Typing here.

Community
  • 1
  • 1
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69