views:

60

answers:

4

If we know the type of variable or parameter very well, why not to declare them?

I'd like to know why it's bad or not necessary.
Sorry, I'm new on Python (from about 1 year) and before I was on C, VB, VB.NET and C# programming languages.

With Python, I hope to have bad parameter types to be catched at compilation time.

And I hope to have an IDE that suggests me every attributes of a variable at design time. May be I'm too Microsoft minded, but variable type declaration seems to be the basic for me.

+2  A: 

That's the nature of dynamic languages.

Static typing is the Java/C# style. (And even C# can infer type using var now.)

It's a tradeoff: you're exchanging type safety for flexibility at runtime.

duffymo
+4  A: 

Ok, I'm sure you know the + function. So, what is it's type? Nubers? Well, it works for lists and strings too. It even works for every object that defines __add__. Or in some cases when one object defines __radd__. So it's hard to tell already. But in Python it is even possible to define these methods at runtime, for example through descriptors or a metaclass. You could even define them basef on user input!

Because Python is so highly dynamic, it's simply impossible for the Python compiler to figure out the type of anything (besides literals) without executing the program. So even if you wrote annotations you would gain nothing, the type checking would still occur at runtime!

THC4k
+1  A: 

Python is strongly-typed so a declaring variable's type is unnecessary. (For obvious reasons you must usually still declare variables!)

The reason for this is because exceptions are raised if you try to do things to types that aren't supported, such as adding an integer to string:

>>> foo = "Hello"
>>> bar = 2
>>> foo + bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

Most other languages do not behave in this way and bad things can happen because of it.

jathanism
Python is indeed strongly typed, but that doesn't make variable declarations unnecessary (or necessary.) It is in fact completely unrelated.
Thomas Wouters
Declaring a variable is indeed important. I should have clarified that you don't declare the object's *type* when the variable is declared.
jathanism
"Strongly typed" is a very weakly defined term, IMO. Different people use it to mean radically different things. "Statically typed" vs "dynamically typed" is much more useful, and there's more consensus on what the terms mean.
Jon Skeet
It really depends on whether you're referencing the variable itself or the value the variable. Variables in Python are dynamic (a variable is just a reference to an object) whereas types are not.
jathanism
@JonSkeet - Its worth noting that, at least in many people's definitions, Strong vs Weak typing and Static vs Dynamic typing are two different axis. While they are related (both having to do with typing), they have a fair amount of orthogonality. I have no doubt you're aware of this, but your comment could be read by someone saying that can be used to mean close to the same thing.
RHSeeger
@RHSeeger: Well they *can* be used to mean close to the same thing. Or they can be used very differently. That's the problem with using such an ambiguous term. Things like static/dynamic, explicit/implicit are more useful - along with precise definitions, of course :)
Jon Skeet
@JonSkeet: My understanding of strong vs weak was how much the language allows you to treat one type as another type (ie, a pointer in C can be operated on as an integer), and deals with Values Static/Dynamic typing deals with Variables (what types of Values a Variable (or name) can hold). Explicit/Implicit deals with how much information you need to provide to the compiler (via the language) to tell it what types things are. I get that strong/weak is used (incorrectly) by some to mean the who enchilada, but there really isn't another terminology for it, is there?
RHSeeger
@RHSeeger: Your idea of "strong/weak" is what I usually refer to as "safe/unsafe". When you claim that others are using weak/strong "incorrectly" - what's your authoritative source to suggest that the way you are using it is "correct" and they are "incorrect"?
Jon Skeet
@JonSkeet: Honestly, just from common language used by the people and sites I frequest, though wikipedia does seem to back it up (http://en.wikipedia.org/wiki/Strong_typing) by saying it's the "most generally" used definition. Along the same lines (http://stackoverflow.com/questions/2351190/static-dynamic-vs-strong-weak). That being said, your statement that it's a less firmly defined terminology is fair.
RHSeeger
+4  A: 

Python uses Dynamic Typing (Duck Typing to be precise) and hence you need not declare a variable. It is a programming and style approach the Developers of Python have chosen. Being a high level language this approach fits into Pythons philosophy of easy readable code.

There are other languages which follow this approach too. (PHP).

sheki
And JavaScript.
duffymo
Be careful not to confuse duck typing (which is based on interfaces) with that of objects being strongly-typed. They are related yet distinct paradigms.
jathanism
There's a whole lot of dynamically-typed languages. And of all of them, you had to name only one, namely the propably most-hated language on the internet?
delnan