I\'ve come across links that say Python is a strongly typed language.
However, I thought in strongly typed languages you couldn\'t do this:
bob = 1
b
According to this wiki Python article Python is both dynamically and strongly typed (provides a good explanation too).
Perhaps you are thinking about statically typed languages where types can not change during program execution and type checking occurs during compile time to detect possible errors.
This SO question might be of interest: Dynamic type languages versus static type languages and this Wikipedia article on Type Systems provides more information
class testme(object):
''' A test object '''
def __init__(self):
self.y = 0
def f(aTestMe1, aTestMe2):
return aTestMe1.y + aTestMe2.y
c = testme #get a variable to the class
c.x = 10 #add an attribute x inital value 10
c.y = 4 #change the default attribute value of y to 4
t = testme() # declare t to be an instance object of testme
r = testme() # declare r to be an instance object of testme
t.y = 6 # set t.y to a number
r.y = 7 # set r.y to a number
print(f(r,t)) # call function designed to operate on testme objects
r.y = "I am r.y" # redefine r.y to be a string
print(f(r,t)) #POW!!!! not good....
The above would create a nightmare of unmaintainable code in a large system over a long period time. Call it what you want, but the ability to "dynamically" change a variables type is just a bad idea...
I just discovered a superb concise way to memorize it:
Dynamic/static typed experssion; strongly/weakly typed value.
Python is strongly, dynamically typed.
As for your example
bob = 1
bob = "bob"
This works because the variable does not have a type; it can name any object. After bob=1
, you'll find that type(bob)
returns int
, but after bob="bob"
, it returns str
. (Note that type
is a regular function, so it evaluates its argument, then returns the type of the value.)
Contrast this with older dialects of C, which were weakly, statically typed, so that pointers and integers were pretty much interchangeable. (Modern ISO C requires conversions in many cases, but my compiler is still lenient about this by default.)
I must add that the strong vs. weak typing is more of a continuum than a boolean choice. C++ has stronger typing than C (more conversions required), but the type system can be subverted by using pointer casts.
The strength of the type system in a dynamic language such as Python is really determined by how its primitives and library functions respond to different types. E.g., +
is overloaded so that it works on two numbers or two strings, but not a string and an number. This is a design choice made when +
was implemented, but not really a necessity following from the language's semantics. In fact, when you overload +
on a custom type, you can make it implicitly convert anything to a number:
def to_number(x):
"""Try to convert function argument to float-type object."""
try:
return float(x)
except (TypeError, ValueError):
return 0
class Foo:
def __init__(self, number):
self.number = number
def __add__(self, other):
return self.number + to_number(other)
Instance of class Foo
can be added to other objects:
>>> a = Foo(42)
>>> a + "1"
43.0
>>> a + Foo
42
>>> a + 1
43.0
>>> a + None
42
Observe that even though strongly typed Python is completely fine with adding objects of type int
and float
and returns an object of type float
(e.g., int(42) + float(1)
returns 43.0
). On the other hand, due to the mismatch between types Haskell would complain if one tries the following (42 :: Integer) + (1 :: Float)
. This makes Haskell a strictly typed language, where types are entirely disjoint and only a controlled form of overloading is possible via type classes.
Python typing is Dynamic so you can change a string variable to an int
x = 'somestring'
x = 50
Python typing is Strong so you can't merge types:
'foo' + 3 --> TypeError: cannot concatenate 'str' and 'int' objects
In weakly-typed Javascript this happens...
'foo'+3 = 'foo3'
Java forces you to explicitly declare your object types
int x = 50;
Kotlin uses inference to realize it's an int
x = 50
But because both languages use static types, x
can't be changed from an int
. Neither language would allow a dynamic change like
x = 50
x = 'now a string'
You are confusing 'strongly typed' with 'dynamically typed'.
I cannot change the type of 1
by adding the string '12'
, but I can choose what types I store in a variable and change that during the program's run time.
The opposite of dynamic typing is static typing; the declaration of variable types doesn't change during the lifetime of a program. The opposite of strong typing is weak typing; the type of values can change during the lifetime of a program.