Puzzled by list-appending behavior
Terry Reedy
tjreedy at udel.edu
Thu May 26 14:44:52 EDT 2011
On 5/26/2011 3:18 AM, Algis Kabaila wrote:
> And why do you insist on calling an instance of list, "list"? Even a
> human reader will confuse which is which. What you are showing is an
> example how confusing things become when a keyword (list) is
> over-written (with list instance).
(Minor note: 'list' is not a keyword (if it were, it could not be
over-ridden) but it is a builtin.) You are correct, it is confusing.
Such usage will also lead to bugs if one ever tries to access the class
as 'list' later in the program.
Here is a legitimate usage of builtins masking:
import builtins
def list(iterable):
print('building list from {}: {}'.format(type(iterable),iterable))
return builtins.list(iterable)
a = list((1,2,3))
b = list({1,2,3})
###
building list from <class 'tuple'>: (1, 2, 3)
building list from <class 'set'>: {1, 2, 3}
--
Terry Jan Reedy
More information about the Python-list
mailing list