Question about references/copies

Arthur ajsiegel at optonline.com
Sat Aug 28 16:43:12 CEST 2004


On Sat, 28 Aug 2004 16:02:59 +0200, aleaxit at yahoo.com (Alex Martelli)
wrote:

>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).

Well there is a lot of divergence in practice, as I think you know.
And beyond that no real consensus on what is preferable, as far as I
have been able to determnine  Though hearing Alex declare it as
preferable is the beginning of the formation of some consensus, one
hopes.

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

I understand, now, the problems with treating the copy module
otherwise than it is.  But it is a strange duck - conceptually in the
middle of things, but with limited functional importance. I thought
only that Ray's idea of setting it off a bit, by mention in the
tutorial, had considerable merit. 
> 
>
>> 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").

I agree that dict(mydict) and list(mylist) -  it that is what one saw
with consitentcy in practice - would go a long way. I'll try to
practice it, having gotten into the list[:] habit.  

And ...

It was my understanding - perhaps wrong - that dict.copy() was an
add-on method to dict of non-ancient origin.  How do I check?

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

That would be excellant!

ARt





More information about the Python-list mailing list