473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Explicit variable declaration

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
Jun 27 '08 #1
11 4368
On Apr 22, 7:39 pm, "Filip Gruszczyński" <grusz...@gmail.comwrote:
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
...

;-)
Jun 27 '08 #2
On Apr 22, 7:39 pm, "Filip Gruszczyński" <grusz...@gmail.comwrote:
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
>
--
Filip Gruszczyński
Jun 27 '08 #3
"Filip GruszczyƄski" <gr******@gmail.comwrites:
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
Jun 27 '08 #4
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
Jun 27 '08 #5
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/

Jun 27 '08 #6
Steve Holden <st***@holdenweb.comwrote:
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
>>>
Jun 27 '08 #7
Lie
On Apr 23, 4:52 pm, "Filip Gruszczyński" <grusz...@gmail.comwrote:
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.
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
Jun 27 '08 #8
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
Jun 27 '08 #9

"Filip Gruszczynski" <gr******@gmail.comwrote in message
news:1b*****************************************@m ail.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

Jun 27 '08 #10

"Filip Gruszczynski" <gr******@gmail.comwrote in message
news:ma*************************************@pytho n.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?
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

Jun 27 '08 #11
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
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Luis Solís | last post by:
Hi It is possible to declare some variables as int, long... ? , or something like visual basic option explicit. In some situations could be usefull. Thanks
7
by: Mats | last post by:
Option Explicit does not work anymore.(?) If you put <%option explicit%> at the top of your pages (direktly after the language declaration) you should get an error for each undeclared variable....
1
by: Tim Marshall | last post by:
For class modules for forms, Option Explicit used to be a default declaration at the top of the form module. It isn't anymore. Should there be anything i need to worry about? This may not be...
2
by: Nils Emil P. Larsen | last post by:
Hello I have read about a C shared library which I want to use in my C program. (It's a library to encode/decode packets from/to a serial bus running with the SNAP-protocol). Unfortunatly...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
26
by: samjnaa | last post by:
Hello. Please tell me whether this feature request is sane (and not done before) for python so it can be posted to the python-dev mailing list. I should say first that I am not a professional...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
1
by: ARC | last post by:
Ok, this is strange. I created a new database and imported all objects from the old program file. Form some reason, the option explicit statement is no longer showing in my code modules (and I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.