Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > behavior varied between empty string '' and empty list []

Reply
Thread Tools

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

 
 
Tzury Bar Yochay
Guest
Posts: n/a
 
      03-24-2008
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?
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-24-2008
En Mon, 24 Mar 2008 15:22:43 -0300, Tzury Bar Yochay
<(E-Mail Removed)> escribió:

> 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:

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

and this is the first part:

py> a = [1,2,3]
py> b = []
py> c = b.extend(a)
py> c
py> b
[1, 2, 3]

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

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying varied types Pat C++ 2 11-25-2009 12:23 AM
URI queries with varied amounts of named values rickle Perl Misc 10 04-05-2009 05:47 PM
Displaying varied pictures in html Steve Freides Computer Support 16 10-24-2008 12:46 PM
How to handle varied length of output signal systolic VHDL 3 10-21-2004 09:03 PM
varied results with Request.ServerVariables("SCRIPT_NAME") Laura ASP General 2 02-18-2004 04:39 PM



Advertisments