Python - why can't I require variable declarations?

KalTorak

Ars Tribunus Militum
2,653
I'm part of a coding project that's written in Perl.
We've acknowledged the world's moved on, plus Perl allowed (sometimes encouraged!) some crummy coding habits.
So the general consensus is we're gonna migrate code to Python.

I didn't realize till today that Python can't be configured to demand variables be declared before use; there's no analog to "use strict vars".

In Perl, it's awfully bad practice NOT to set "use strict", and our defect rate definitely improved as we moved more of our codebase to set it.

What's the rationale behind Python's choice not to allow the dev to force variable declarations?
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Why questions on intent are hard without authoritative statements from the designer(s)

That said a read through the comments and answers on this question will cover much of the debate which pretty much boils down to the opinions “It should get trapped by a unit test” vs “the compiler checking it is less hassle”
https://stackoverflow.com/questions/221 ... -variables

I cannot speak to the merits of those arguments from experience because use of python for anything large drives me up the wall anyway because I am extremely reliant on the compiler forcing the flow of required changes through a program. This may be my mindset being ossified after decades of use of largely statically typed languages though.
 

zeotherm

Ars Legatus Legionis
10,960
Moderator
I cannot speak to the merits of those arguments from experience because use of python for anything large drives me up the wall anyway because I am extremely reliant on the compiler forcing the flow of required changes through a program. This may be my mindset being ossified after decades of use of largely statically typed languages though.
As someone who does 75% strongly statically typed language and 20% python and 5% Java, I MUCH MUCH MUCH prefer the strong type checking given by the compiler. Yes, Python is great for knocking out an idea, but once something gets above a certain size I just don't see it as manageable. And I admit that that is personal bias, but I have come to love a strong compiler that helps guarantee correctness.
 
D

Deleted member 43669

Guest
I've done a lot of Python and Java.

Where I miss the static typing is IDE features; refactoring, completion, etc. That sucks.

But not on actual type errors, or misnamed variables, etc. It's true that testing catches them, and it doesn't require much more testing than on a statically-typed language, IMHO, if you do it right. It probably has a cost- and I don't think it has significant benefits to offset this cost, but it could be worse (for instance, using Django and its admin can make up for this cost easily in many projects).

But again... insufficient code completion? No sophisticated refactor? No quick navigation? Ugh, that's a harder pill to swallow.

edit: to expand on this, when I did Java before generics, I don't think I had significant errors due to that. But again, it weakened tooling a lot.
 

Deffexor

Ars Tribunus Angusticlavius
7,607
Moderator
What's the rationale behind Python's choice not to allow the dev to force variable declarations?

Having programmed Python for the past few years, I don't think requiring variable declarations before use gets you anything in Python. You can do it if you want, though.

Python is also strongly (and dynamically) typed whereas Perl is weakly (and dynamically) typed. It doesn't really matter to the Python interpreter when/where you declare a variable.

If you're using Python 3.5 or higher, you should look into Type Hinting. In short, you can declare the type, like in a statically typed language, but that type is not enforced at run-time. The IDE (like PyCharm) can pick up on it while you write your code and underline things that it thinks you're doing wrong. But it still allows you to jam one thing into the next if you feel like it, types be damned.

For the most part, I write Object Oriented Python, so generally, stuff is already declared inside the class with the constructor.

Does this help?
 

Deffexor

Ars Tribunus Angusticlavius
7,607
Moderator
What's the rationale behind Python's choice not to allow the dev to force variable declarations?

Having programmed Python for the past few years, I don't think requiring variable declarations before use gets you anything in Python. You can do it if you want, though.

Python is also strongly (and dynamically) typed whereas Perl is weakly (and dynamically) typed. It doesn't really matter to the Python interpreter when/where you declare a variable.

If you're using Python 3.5 or higher, you should look into Type Hinting. In short, you can declare the type, like in a statically typed language, but that type is not enforced at run-time. The IDE (like PyCharm) can pick up on it while you write your code and underline things that it thinks you're doing wrong. But it still allows you to jam one thing into the next if you feel like it, types be damned.

For the most part, I write Object Oriented Python, so generally, stuff is already declared inside the class with the constructor.

Does this help?