Question about references/copies

Alex Martelli aleaxit at yahoo.com
Sat Aug 28 16:02:59 CEST 2004


Arthur <ajsiegel at optonline.com> wrote:
   ...
> I've felt strongly that this key piece of Python learning would be
> and much, much more inevitable if there was a (preferred) consistent
> way to ask for a copy, across objects - 

There is: type(obj)(obj) is (almost) always the best way to get a
(shallow) copy of copyable objects (won't work with noncopyables such as
files and other iterators, of course) of built-in types (whether
user-coded types support that popular convention is of course another
issue -- it depend on the author of each of these types).

If you know what type obj is, say a list; or, if don't really care
whether obj is (say) a list or a tuple or ... 'cause what you want is a
list anyway, then the normal way to spell this is of course list(obj).

> and/or that the copy module
> was something other than one of XXX importable moudles.

Well, it's Python-coded, so it seems quite natural to me that it be a
perfectly normal importable module.
 

> That dicts and lists (for example) have totally different syntax for
> copy, helps - I promise - to misdirect and confuse. I happen to

dict(mydict) and list(mylist) appear quite similar to me.  Sure,
mydict.copy() has stuck around from the dark ages and won't be even
considered for removal, due to backwards compatibility, until Python 3.0
some years from now; and mylist[:] also builds a shallow copy; but
despite their undeserved popularity these are not "the syntax for copy"
in my book, any more than, say, dict(mydict.iteritems()) for a dict or
[x for x in mylist] for a list, even though THOSE happen to make shallow
copies too (and I HAVE seen them used in code intended as "serious").

> consider this entire area a significant wart. I think I am entitled to
> consider it so. 

Given the apparent dearth of information in the matter, you may be
right.  I'll do my best to keep clarifying my viewpoint in my books...

> I had tried to bring this up to in various forums, most particularly
> edu-sig - where I thought this knid of discussion might be of
> particular significance - and was rather rudely asked by Guido to take
> it elsewhere.  Where? not clear.

Not sure why edu-sig would be more appropriate than any generic Python
forum, actually.  But not particularly inappropriate either, I think.


Alex



More information about the Python-list mailing list