coding style - where to declare variables

Richard Damon Richard at Damon-family.org
Sun Jul 22 09:36:13 EDT 2018


> On Jul 22, 2018, at 8:48 AM, Sharan Basappa <sharan.basappa at gmail.com> wrote:
> 
> In other programming languages (e.g. C, C++), as a good practice, variables are declared right at the start of the program, irrespective of where it is normally used. What is the practice in Python?
> 
> I see that, most of the code, declare variables where it is used and not at the start of the program.

I would disagree that it is universally considered good practice to declare everything at the front. (There is much disagreement on this, but most consider declare at first use to be preferred, where possible).

Last century, C code required this to make things easier on the compiler, and some code bases and examples go back that far, so they use that method out of inertia.  


More information about the Python-list mailing list