Why now static data for functions?

A forum for general discussion of the Python programming language.

Why now static data for functions?

Postby russ » Tue Mar 05, 2013 8:36 am

if I have a function that relies on some data, I either put the data inside the function definition or in the global scope right above the function so it's a little more obvious where it belongs. But the first is inefficient and the second is ugly.

If you do:
Code: Select all
def func():
    x = range(10**7)
    print x[0]


it's going to create the list from scratch every time you call the function. If you type that in the interpreter and manually call it you'll see it takes some fraction of a second each time.

if you do:
Code: Select all
x = range(10**7)
def func():
    print x[0]

It looks a little messy and isn't it bad practice to have a function which depends on data in the global scope?

Then I found a workaround but accident which is also just as ugly but it keeps the data in the local scope and creates it only once when the function definition is read. You can even change it.

Code: Select all
def func(arg=range(10**7)):
    arg[0] += 1
    print arg[0]

output:
Code: Select all
>>> x()
1
>>> x()
2
>>> x()
3
>>>


It takes a little longer for some reason when the function is created, but after that there is no delay.

So why can't there just be

Code: Select all
def func():
    @static  x = range(10**7)


...or something instead of having to use classes?

Does any language support this?
russ
 
Posts: 18
Joined: Sat Mar 02, 2013 8:59 am

Re: Why now static data for functions?

Postby setrofim » Tue Mar 05, 2013 9:18 am

russ wrote:isn't it bad practice to have a function which depends on data in the global scope?

Yes, it is. This doesn't mean that you mustn't ever do it, but you should avoid global (mutable) data whenever possible.

russ wrote:So why can't there just be

Code: Select all
def func():
    @static  x = range(10**7)


...or something instead of having to use classes?


Because that's precisely what classes are for -- to associate data with behaviour (functions) and keep track of state.
Besides, would that really be any less ugly than
Code: Select all
def func(x=range(10**7)):
   ...

?

russ wrote:Does any language support this?

Yes, C/C++ support static function variables, though you rarely see them in C++ because, typically, classes are used instead.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Why now static data for functions?

Postby micseydel » Tue Mar 05, 2013 8:29 pm

I can appreciate your desire for a "static" keyword in functions, but you can override the __call__ method in a class to get the same behavior in the program afterward.

The idea is, functions should avoid side effects other than I/O as much as possible, and classes are entirely appropriate for preserving state.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1358
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests