Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ & Java, and I am wondering how in the world I could just declare variables like id = 0
and name = 'John'
without any int
's or string
's in front! I figured out that perhaps it's because there are no '
's in a number, but how would Python figure that out in something like def increase(first, second)
instead of something like int increase(int first, int second)
in C++?!
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
When a variable is created by an assignment such as variable=value, Python determines and assigns a data type to the variable. A data type defines how the variable is stored and the rules governing how the data can be manipulated. Python uses the variable's assigned value to determine its type.
The literal objects you mention carry (pointers to;-) their own types with them of course, so when a name's bound to that object the problem of type doesn't arise -- the object always has a type, the name doesn't -- just delegates that to the object it's bound to.
There's no "figuring out" in def increase(first, second):
-- name increase
gets bound to a function object, names first
and second
are recorded as parameters-names and will get bound (quite possibly to objects of different types at various points) as increase
gets called.
So say the body is return first + second
-- a call to increase('foo', 'bar')
will then happily return 'foobar'
(delegating the addition to the objects, which in this case are strings), and maybe later a call to increase(23, 45)
will just as happily return 68
-- again by delegating the addition to the objects bound to those names at the point of call, which in this case are ints. And if you call with incompatible types you'll get an exception as the delegated addition operation can't make sense of the situation -- no big deal!
Python is dynamically typed: all variables can refer to an object of any type. id
and name
can be anything, but the actual objects are of types like int
and str
. 0
is a literal that is parsed to make an int
object, and 'John'
a literal that makes a str
object. Many object types do not have literals and are returned by a callable (like frozenset
—there's no way to make a literal frozenset, you must call frozenset
.)
Consequently, there is no such thing as declaration of variables, since you aren't defining anything about the variable. id = 0
and name = 'John'
are just assignment.
increase
returns an int
because that's what you return in it; nothing in Python forces it not to be any other object. first
and second
are only ints if you make them so.
Objects, to a certain extent, share a common interface. You can use the same operators and functions on them all, and if they support that particular operation, it works. It is a common, recommended technique to use different types that behave similarly interchangably; this is called duck typing. For example, if something takes a file
object you can instead pass a cStringIO.StringIO
object, which supports the same method as a file (like read
and write
) but is a completely different type. This is sort of like Java interfaces, but does not require any formal usage, you just define the appropriate methods.
Python uses the duck-typing method - if it walks, looks and quacks like a duck, then it's a duck. If you pass in a string, and try to do something numerical on it, then it will fail.
Have a look at: http://en.wikipedia.org/wiki/Python_%28programming_language%29#Typing and http://en.wikipedia.org/wiki/Duck_typing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With