470,614 Members | 1,556 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

behavior varied between empty string '' and empty list []

while I can invoke methods of empty string '' right in typing
(''.join(), etc.) I can't do the same with empty list

example:
>>a = [1,2,3]
b = [].extend(a)
b
b = []
b.extend(a)
b
[1,2,3]

I would not use b = a since I don't want changes on 'b' to apply on
'a'

do you think this should be available on lists to invoke method
directly?
Mar 24 '08 #1
1 1297
En Mon, 24 Mar 2008 15:22:43 -0300, Tzury Bar Yochay
<Af**********@gmail.comescribió:
while I can invoke methods of empty string '' right in typing
(''.join(), etc.) I can't do the same with empty list

example:
>>>a = [1,2,3]
b = [].extend(a)
b
b = []
b.extend(a)
b
[1,2,3]
extend() -like most mutating methods- does not return the list, it returns
None.
Your empty list grow the 3 additional items, but since there were no
additional references to it, got destroyed.
I would not use b = a since I don't want changes on 'b' to apply on
'a'
Try with b = list(a)
do you think this should be available on lists to invoke method
directly?
You already can. Your example is misleading because you used b with two
meanings.
(Compare the *usage* of each variable/value, not their names). This is
equivalent to the second part of your example:

pya = [1,2,3]
pyb = []
pyb.extend(a)
pyb
[1, 2, 3]

and this is the first part:

pya = [1,2,3]
pyb = []
pyc = b.extend(a)
pyc
pyb
[1, 2, 3]

except that in your original example, the empty list had no name so you
cannot see how it changed.

--
Gabriel Genellina

Mar 24 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Danny Anderson | last post: by
1 post views Thread by KPS | last post: by
3 posts views Thread by Larry Lard | last post: by
12 posts views Thread by Laurent Deniau | last post: by
4 posts views Thread by kghose | last post: by
4 posts views Thread by Gabriel Rossetti | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.