Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80605
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Curious function argument |
| Date | 2014-11-12 09:18 -0500 |
| Organization | IISS Elusive Unicorn |
| References | <54635bce$0$2384$426a74cc@news.free.fr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15721.1415801914.18130.python-list@python.org> (permalink) |
On Wed, 12 Nov 2014 14:07:14 +0100, "ast" <nomail@invalid.com> declaimed
the following:
>Hello
>
>I saw in a code from a previous message in this forum
>a curious function argument.
>
>def test(x=[0]):
> print(x[0]) ## Poor man's object
> x[0] += 1
>
>>>> test()
>0
>>>> test()
>1
>>>> test()
>2
>>>>
>
>I understand that the author wants to implement a global
>variable x . It would be better to write 'global x' inside the
>function.
>
No... The author implemented a mutable DEFAULT argument.
The argument can still be overloaded by passing something else into the
function.
y = [0]
z = 234
test(y)
test(z)
This would not work if one instead declared a global "x"
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Curious function argument "ast" <nomail@invalid.com> - 2014-11-12 14:07 +0100 Re: Curious function argument Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-12 14:03 +0000 Re: Curious function argument Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-12 09:18 -0500 Re: Curious function argument Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-15 21:39 +1100 Re: Curious function argument Akira Li <4kir4.1i@gmail.com> - 2014-11-28 08:52 +0300
csiph-web