Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Explicit variable declaration

6 views
Skip to first unread message

Filip Gruszczyński

unread,
Apr 22, 2008, 8:39:41 PM4/22/08
to pytho...@python.org
Hello everyone!

It is my first message on this list, therefore I would like to say
hello to everyone. I am fourth year student of CS on the Univeristy of
Warsaw and recently I have become very interested in dynamically typed
languages, especially Python.

I would like to ask, whether there is any way of explicitly declaring
variables used in a function? While I am pretty sure, that there is no
such way in the language itself, I would like to know, whether there
are any third-party tools to do that. This would be very useful for me
during development, so I am looking for such a tool.

--
Filip Gruszczyński

Dan Bishop

unread,
Apr 22, 2008, 10:27:22 PM4/22/08
to

def a_function(args):
# Local variables: item, item2
...

;-)

Benjamin

unread,
Apr 22, 2008, 10:45:03 PM4/22/08
to
On Apr 22, 7:39 pm, "Filip Gruszczyński" <grusz...@gmail.com> wrote:

You mean the type? Not in 2.x, but in 3.x, there are function
annotations:

def a_function(arg1: int, arg2: str) -> None: pass

>
> --
> Filip Gruszczyński

Ben Finney

unread,
Apr 22, 2008, 11:01:49 PM4/22/08
to
"Filip Gruszczyński" <grus...@gmail.com> writes:

> I have become very interested in dynamically typed languages,
> especially Python.

Good to know. Welcome to the group.

> I would like to ask, whether there is any way of explicitly
> declaring variables used in a function?

Declaring what about them? If you mean declaring the type, remember
that Python deliberately allows any name to be bound to any object;
type declarations can't be enforced without losing a lot of the power
of Python.

--
\ "Hanging one scoundrel, it appears, does not deter the next. |
`\ Well, what of it? The first one is at least disposed of." -- |
_o__) Henry L. Mencken |
Ben Finney

Gabriel Genellina

unread,
Apr 23, 2008, 1:10:02 AM4/23/08
to pytho...@python.org
En Tue, 22 Apr 2008 21:39:41 -0300, Filip Gruszczyński
<grus...@gmail.com> escribió:

> Hello everyone!
>
> It is my first message on this list, therefore I would like to say
> hello to everyone. I am fourth year student of CS on the Univeristy of

> Warsaw and recently I have become very interested in dynamically typed
> languages, especially Python.

Welcome to Python!

> I would like to ask, whether there is any way of explicitly declaring

> variables used in a function? While I am pretty sure, that there is no
> such way in the language itself, I would like to know, whether there
> are any third-party tools to do that. This would be very useful for me
> during development, so I am looking for such a tool.

Not part of the language itself, but there are some tools to do static
code analysis, like PyLint and PyChecker; see
http://wiki.python.org/moin/static_source_analysis

--
Gabriel Genellina

Filip Gruszczyński

unread,
Apr 23, 2008, 5:52:28 AM4/23/08
to pytho...@python.org
> You mean the type? Not in 2.x, but in 3.x, there are function
> annotations:
>
> def a_function(arg1: int, arg2: str) -> None: pass

Nope, I don't like types ;-) 3.x seems pretty revolutionary, and this
typing can be appreciated by some people.

> Declaring what about them? If you mean declaring the type, remember
> that Python deliberately allows any name to be bound to any object;
> type declarations can't be enforced without losing a lot of the power
> of Python.

Just declaring, that they exist. Saying, that in certain function
there would appear only specified variables. Like in smalltalk, if I
remember correctly.

--
Filip Gruszczyński

Steve Holden

unread,
Apr 23, 2008, 8:01:25 AM4/23/08
to pytho...@python.org
Filip Gruszczyński wrote:
>> You mean the type? Not in 2.x, but in 3.x, there are function
>> annotations:
>>
>> def a_function(arg1: int, arg2: str) -> None: pass
>
> Nope, I don't like types ;-) 3.x seems pretty revolutionary, and this
> typing can be appreciated by some people.
>
>> Declaring what about them? If you mean declaring the type, remember
>> that Python deliberately allows any name to be bound to any object;
>> type declarations can't be enforced without losing a lot of the power
>> of Python.
>
> Just declaring, that they exist. Saying, that in certain function
> there would appear only specified variables. Like in smalltalk, if I
> remember correctly.
>
Icon has (had?) the same feature: if the "local" statement appeared then
the names listed in it could be assigned in the local namespace, and
assignment to other names wasn't allowed.

A lot of people assume that's what the __slots__ feature of the new
object model is for, but it isn't: it's actually a memory conservation
device for circumstances when millions of objects must be created in an
application.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Duncan Booth

unread,
Apr 23, 2008, 11:10:49 AM4/23/08
to
Steve Holden <st...@holdenweb.com> wrote:

> Filip GruszczyŹ"ski wrote:
>> Just declaring, that they exist. Saying, that in certain function
>> there would appear only specified variables. Like in smalltalk, if I
>> remember correctly.
>>
> Icon has (had?) the same feature: if the "local" statement appeared
then
> the names listed in it could be assigned in the local namespace, and
> assignment to other names wasn't allowed.

Python being what it is, it is easy enough to add support for declaring
at the top of a function which local variables it uses. I expect that
actually using such functionality will waste more time than it saves,
but here's a simple enough implementation:

def uses(names):
def decorator(f):
used = set(f.func_code.co_varnames)
declared = set(names.split())
undeclared = used-declared
unused = declared-used
if undeclared:
raise ValueError("%s: %s assigned but not declared"
% (f.func_name, ','.join(undeclared)))
if unused:
raise ValueError("%s: %s declared but never used"
% (f.func_name, ','.join(unused)))
return f
return decorator

Used something like this:

>>> @uses("x y")
def f(x):
y = x+1
return z

>>> @uses("x y z")
def f(x):
y = x+1
return z


Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
@uses("x y z")
File "<pyshell#32>", line 10, in decorator
raise ValueError("%s: %s declared but never used" % (f.func_name,
','.join(unused)))
ValueError: f: z declared but never used
>>> @uses("x")
def f(x):
y = x+1
return z


Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
@uses("x")
File "<pyshell#32>", line 8, in decorator
raise ValueError("%s: %s assigned but not declared" % (f.func_name,
','.join(undeclared)))
ValueError: f: y assigned but not declared
>>>

Filip Gruszczyński

unread,
Apr 23, 2008, 12:13:34 PM4/23/08
to pytho...@python.org
Wow! This is extremely easy and seems to do exactly what I need. Those
decorators are pretty powerful then. Thanks for your help, I'll try to
use this.

> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
Filip Gruszczyński

Lie

unread,
Apr 23, 2008, 12:48:54 PM4/23/08
to

If you want to just declare that name exist, but doesn't want to
declare the type, why don't you just do this:

def somefunc():
nonlocal = nonlocal
local = 0 # or None or [] or an initial value
#
return nonlocal * local

Filip Gruszczyński

unread,
Apr 23, 2008, 9:29:54 PM4/23/08
to pytho...@python.org
> If you want to just declare that name exist, but doesn't want to
> declare the type, why don't you just do this:
>
> def somefunc():
> nonlocal = nonlocal
> local = 0 # or None or [] or an initial value
> #
> return nonlocal * local

Err.. I don't quite get. How it may help me? Could you explain?

--
Filip Gruszczyński

Terry Reedy

unread,
Apr 24, 2008, 4:40:25 AM4/24/08
to pytho...@python.org

"Filip Gruszczynski" <grus...@gmail.com> wrote in message
news:1be78d220804231829k2c...@mail.gmail.com...

|> If you want to just declare that name exist, but doesn't want to
| > declare the type, why don't you just do this:

Names do not 'exist' in Python, nor do they have types. They are bound to
objects that have types. Learn to program Python as Python, not one of
those languages with a quite different model of names and values.

| > def somefunc():
| > nonlocal = nonlocal

Syntax error in 3.0. Error or nonsense in 2.x

| > local = 0 # or None or [] or an initial value
| > #
| > return nonlocal * local
|

| Err.. I don't quite get. How it may help me? Could you explain?

Forget the above. The only 'declarations' in Python, 'global' and
'nonlocal' are for the specialized purpose of *binding* names that are not
in the local namespace of a function or nested function. They are only
needed because otherwise names that get bound are otherwise assumed to be
local. See the language ref section on function defs.

tjr

Jason Stokes

unread,
Apr 25, 2008, 1:23:36 AM4/25/08
to

"Filip Gruszczynski" <grus...@gmail.com> wrote in message
news:mailman.89.12090006...@python.org...

Hi Filip,

In Python the standard patten for "declaring" variables is just to assign to
them as they are needed. If you want the effect of a declaration as you
would do in C, you can just define the variable and initialize it to 0 or
None. (Or {} for a new dictionary, or [] for a new list.)

eg,

def collaterecs():
recordscount = 0
recordlist = []
...
return recordlist

Filip Gruszczyński

unread,
Apr 25, 2008, 12:23:43 PM4/25/08
to pytho...@python.org
> In Python the standard patten for "declaring" variables is just to assign to
> them as they are needed. If you want the effect of a declaration as you
> would do in C, you can just define the variable and initialize it to 0 or
> None. (Or {} for a new dictionary, or [] for a new list.)

Yep, I get it and I absolutely love this about Python. What I don't
actually love is situation, where I misspell and instead of setting
one variable, other is created. This is why I would like to declare
variables first and say, that these are the only ones, that can be set
in certain function (the same with fields in a class).

I am finishing a small tool, that allows to create such declarations
in similar manner to Smalltalk declarations. I hope I can post a link
to it soon.

--
Filip Gruszczyński

0 new messages