Python Newbie

Michael Torrie torriem at gmail.com
Mon Feb 25 02:31:38 CET 2013


On 02/24/2013 03:43 PM, piterrr.dolinski at gmail.com wrote:
> I wanted Python to register what type of variable I'm after. So I
> init my vars accordingly, int might be 0, float 0.0 and string with
> null, err... None.

As several people on the list have pointed out, there are no variables
in Python.  Let me repeat that.  There are no variables in python.  Thus
to say, x=5 does not tell anything about what x can represent in the
future.  It merely says that the *name* "x" is bound to the object,
which happens to be an immutable integer object that represents 5.  That
5 can never change.  Ever.   In the future you can assign x to another
object, maybe the result of an expression.  So none of what you did
"initialializes" a "variable."  Probably sounds like I'm just being
pedantic, but if you can learn to see the wisdom and strengths of
python's way of doing things you'll end up writing very rapid code and
very correct code too.

Python's type system is dynamic but it is, in fact, a strong type
system.  You can't just arbitrary convert an object from one type to
another.  The aspect of python that gives it so much power over
statically-typed languages is that as long as a type supports the
interface you want to work with, the type just doesn't matter.  Nor
should it.  This allows tremendous code re-use.  For example I can
totally change out one object for another object of a completely
different type and my algorithms and logic still work.  It's similar to
C# generics, but much more powerful.  In Python, it's called
duck-typing.  It's a very powerful concept.



More information about the Python-list mailing list