Bug / Dumb Question a = b= []

56 views
Skip to first unread message

Mark Billion

unread,
Dec 8, 2015, 10:01:36 AM12/8/15
to web2py-users
in python 2.6, I can run a = b = [] and get the expected result.  In W2P, when I do it, it throws an exception as the list does not propagate to both a & b.  It would make my code cleaner, but otherwise not a problem.  Just wanted to give a shout in case anyone gave a ....

Thanks again for all of your help!  

Richard Vézina

unread,
Dec 8, 2015, 10:24:56 AM12/8/15
to web2py-users
idiomatic is not : a, b = [], []

?

Richard

On Tue, Dec 8, 2015 at 10:01 AM, Mark Billion <markb...@gmail.com> wrote:
in python 2.6, I can run a = b = [] and get the expected result.  In W2P, when I do it, it throws an exception as the list does not propagate to both a & b.  It would make my code cleaner, but otherwise not a problem.  Just wanted to give a shout in case anyone gave a ....

Thanks again for all of your help!  

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Dec 8, 2015, 10:51:20 AM12/8/15
to web2py-users
That works fine in web2py, as does any other valid Python code. Must be something else you are doing wrong. Hard to say without seeing what you are really doing.

Anthony

Ron McOuat

unread,
Dec 9, 2015, 3:25:53 PM12/9/15
to web2py-users
Are you sure you want to do that?

a = b = []

binds a and b to the same empty list object so whatever you do to a will also appear in b because they both reference the same list object. Assignment does not create a new object, it simply binds a variable name on the left to the object on the right.

Try this:

a = b = []
a.insert(0,2)
print a, b

should print
[2] [2]

A great source of bugs if a and b are thought to be different objects.

Ron

Richard Vézina

unread,
Dec 9, 2015, 3:46:00 PM12/9/15
to web2py-users
Python idiom should be : a, b = [], []

I remember having learn that in an presentation about idiomatic python, in this presentation I think : https://www.youtube.com/watch?v=OSGv2VnC0go&list=WL&index=14

:)

Richard

--

Richard Vézina

unread,
Dec 9, 2015, 4:02:22 PM12/9/15
to web2py-users
Around 33 minutes...

Mark Billion

unread,
Dec 11, 2015, 4:42:30 PM12/11/15
to web...@googlegroups.com
**** -- thats a really good point!  I did not mean to do that!

On Wed, Dec 9, 2015 at 3:25 PM, Ron McOuat <ron.m...@gmail.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/xCXgIDUZVnU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Mark M. Billion
1904 N. Lincoln
Wilmington, DE 19806
302.416.2199
Reply all
Reply to author
Forward
0 new messages