Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PythonGoto page 1, 2, 3  Next

Should Python raise a warning for mutable default arguments?

 
Jump to:  
 
Steven D'Aprano
PostPosted: Fri Aug 22, 2008 12:42 pm    Post subject: Should Python raise a warning for mutable default arguments?
       
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully, fade
away than another one starts up.

I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.

What do people think?


--
Steven
 

 
Diez B. Roggisch
PostPosted: Fri Aug 22, 2008 12:49 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
Steven D'Aprano schrieb:
Quote:
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully, fade
away than another one starts up.

I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.

I just suggested a documentation enhancement in one of the plethora of
threads... so I'm certainly +1 for any enhancement in this area.

Diez
 

 
Christian Heimes
PostPosted: Fri Aug 22, 2008 1:09 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
Steven D'Aprano wrote:
Quote:
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.

What do people think?

I don't see a chance for your proposal. How are you going to detect
mutable objects? Custom types can be mutable as well as immutable.

Christian
 

 
Peter Otten
PostPosted: Fri Aug 22, 2008 1:32 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
Christian Heimes wrote:

Quote:
Steven D'Aprano wrote:
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.

What do people think?

-0 from me. I'd rather feature it more prominently in the tutorial, a
section "The five most common pitfalls" or something like that.

Quote:
I don't see a chance for your proposal. How are you going to detect
mutable objects? Custom types can be mutable as well as immutable.

A check at compilation time for list literals would catch 90 % of the cases.
The warning would be targeted at newbies after all. It might still be a
source of confusion when they try to import someone else's code that uses
mutable defaults intentionally.

Peter
 

 
Peter Otten
PostPosted: Fri Aug 22, 2008 1:55 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
bearophileHUGS@lycos.com wrote:

Quote:
DrScheme is an implementation of Scheme that is very newbie-friendly.
It has several limited sub-languages, etc.

So maybe a command line option can be added to Python3 ( -
newbie ? Smile ) that just switches on similar warnings, to help newbies
(in schools, where there's a teacher that encourages to always use
that command line option) avoid some of the most common traps.

Or maybe bundle pychecker with idle?

$ cat tmp.py
def test(x, a=[]):
a.append(x)
return a

for i in range(5):
print test(i)

$ pychecker tmp.py
Processing tmp...
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]

Warnings...

tmp.py:2: Modifying parameter (a) with a default value may have unexpected
consequences

Though it might be interesting to ask a newbie what he expects when warned
of "unexpected consequences" ;)

Peter
 

 
Guest
PostPosted: Fri Aug 22, 2008 3:43 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
DrScheme is an implementation of Scheme that is very newbie-friendly.
It has several limited sub-languages, etc.

So maybe a command line option can be added to Python3 ( -
newbie ? Smile ) that just switches on similar warnings, to help newbies
(in schools, where there's a teacher that encourages to always use
that command line option) avoid some of the most common traps.

Bye,
bearophile
 

 
Emile van Sebille
PostPosted: Fri Aug 22, 2008 4:14 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
Steven D'Aprano wrote:
Quote:
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully, fade
away than another one starts up.

I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.

What do people think?



-1

People that have worked through the tutorial, something everyone should
do when they're starting out, will find this explicitly discussed. See

LINK

People that just skim the surface get stung -- sorry.

Emile
 

 
Christian Heimes
PostPosted: Fri Aug 22, 2008 4:41 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
MRAB wrote:
Quote:
Could there be a new special method __mutable__?

Why should we add a new method?

Seriously, why should Python be cluttered and slowed down to warn about
mutable arguments. It's neither a design flaw nor a surprising feature
*ONCE* you have understood how functions work. I agree that the behavior
may be surprising for a newbie but it's logical once you got the big
picture.

Christian
 

 
castironpi
PostPosted: Fri Aug 22, 2008 4:53 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
On Aug 22, 9:42 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
Quote:
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully, fade
away than another one starts up.

I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.

What do people think?

--
Steven


I like the idea of calling it to your attention. +1.

If a warning, you should be able to silence it with an annotation,
decorator, or a module-level flag.

Or perhaps, require it be declared explicit, and make it an error.

def test(x, a=defmut([])):

Python raises an actual error unless default arguments are known
immutable or instances of 'defmut'.
 

 
Diez B. Roggisch
PostPosted: Fri Aug 22, 2008 5:14 pm    Post subject: Re: Should Python raise a warning for mutable default argume
       
Emile van Sebille schrieb:
Quote:
Steven D'Aprano wrote:
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully,
fade away than another one starts up.

I suggest that Python should raise warnings.RuntimeWarning (or
similar?) when a function is defined with a default argument
consisting of a list, dict or set. (This is not meant as an exhaustive
list of all possible mutable types, but as the most common ones that I
expect will trip up newbies.) The warning should refer to the relevant
FAQ or section in the docs.

What do people think?



-1

People that have worked through the tutorial, something everyone should
do when they're starting out, will find this explicitly discussed. See

LINK

People that just skim the surface get stung -- sorry.

But obviously enough, it's not emphazized enough. Even if the
interpreter isn't touched, at least the docs should be.

Diez
 

Page 1 of 3 .:. Goto page 1, 2, 3  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

pobieranie linkow system wymiany linkow proces pobierania linkow trwa pobierania linkow wymiana linkami