Explicit variable declaration

  • Thread starter Filip GruszczyÅ„ski
  • Start date
F

Filip Gruszczyński

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.
 
D

Dan Bishop

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.

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

;-)
 
B

Benjamin

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.

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
 
F

Filip Gruszczyński

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.
 
S

Steve Holden

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


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
 
L

Lie

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


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

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
 
F

Filip Gruszczyński

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?
 
T

Terry Reedy

|> 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
 
F

Filip Gruszczyński

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top