[Tutor] using lists as values for key in dictionary?
wescpy at gmail.com
Wed Oct 15 20:27:26 CEST 2008
>> >> so if you turn your list into a tuple (I can never remember, is that a
>> >> cast or a coercion?)
>> >> lX = tuple(lX)
>> > To answer my own question: neither, but it's closer (in spirit) to a
>> > cast.
>> you're keeping everyone is suspense! :-) you are creating a brand new
>> tuple by copying out the references to the objects in the list. then
>> you assign that tuple to the variable that previously referenced your
>> original list.
>> NOTE: the original object remained as a list with no modifications,
>> but was probably marked for deallocation when you dropped your
>> references to it unless there were other references to it.
> Yes - if I had stopped and thought for just a few more seconds, I would
> never have said that in the first place. tuple() is a function like any
> other; assigning its return value to an object of the same name as its input
> obviously has nothing to do with either casting or coercion and it was a
> dumb thing to say. Which was why I retracted it almost immediately.
tuple() is *almost* like any other built-in function... its main magic
is that it's a *factory* function, one who's name is really a type and
so is the return value because if generates a new object of that type.
> However, what I meant by "closer in spirit" - and it was still a dumb thing
> to say - was that, if the whole thing happened in a black box and you had no
> idea of the mechanics, it would have the "flavor" of a cast, as opposed to
> the "flavor" of coercion. Sorta. Not really. Unfortunately, thanks to the
> magic of the Internet, I can't just ask everybody to forget I mentioned
> it... can I? Please?
you response will outlive you on google forever... your grandkids will
see it, and so will their's... :-)
the good news is that you're familiar with coercion, something that
newbies to Python may not be fully aware of, which is to convert all
numbers to the same type so that you can perform an operation on it --
recall that in most languages (not just Python) that operations are
usually only supported if both operands are of the same type:
>>> 1.5 + 3
you may *think* you're adding an integer 3 to float 1.5, but
internally, it gets converted, oops, i mean *coerced* into a float to
perform the math and give you the result. at the risk of showing
people a function that will *no longer exist* in Python 3.x, this
example is purely to show what happens internally:
>>> coerce(1.5, 3)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
More information about the Tutor