Implicit conversion to boolean in if and while statements

88888 Dihedral dihedral88888 at googlemail.com
Tue Feb 12 21:43:42 EST 2013


Rick Johnson於 2013年2月13日星期三UTC+8上午1時48分07秒寫道:
> On Monday, February 11, 2013 11:55:19 PM UTC-6, Chris Angelico wrote:
> 
> > On Tue, Feb 12, 2013 at 12:06 PM, 88888 Dihedral wrote:
> 
> > > A permanently mutated list is a tuple of constant objects.
> 
> >
> 
> > I nominate this line as "bemusing head-scratcher of the week".
> 
> 
> 
> Actually the statement is fact IF you can grok it through the eyes of clarity.
> 
> 
> 
> "A permanently mutated list..."
> 
> 
> 
> A list that has been mutated permanently, that is, it cannot be changed back into a list. psst: i have a sneaking suspicion that he his referring to tuples, let's see.
> 
> 
> 
> "...is a tuple..."
> 
> 
> 
> Ha! Well in Python the immutable sequence type /is/ a tuple after all.
> 
> 
> 
> "...of constant objects..."
> 
> 
> 
> The tuple contains objects, and it's objects will maintain a constant ordering (relatively in tuple structure) until until the tuple's death. 
> 
> 

>>> a1=[1,2,3]
>>> tuple1=(a1,4,5,6)
>>> tuple1
([1, 2, 3], 4, 5, 6)
>>> a1=[1,2]
>>> tuple1
([1, 2, 3], 4, 5, 6)
>>> 

Yes, a tuple of constant objects is still not clear. 


> 
> Your confusion may stem from interpreting "constant" as the CS term "CONSTANT"[1]; whereby the objects in the tuple are programming CONSTANTS, that is, unable to change. But in reality, although a tuple (bka:StaticList) cannot expand to add more objects, or shrink to eject existing objects, the objects themselves CAN change their own internal state WITHOUT disrupting the immutable harmony of the tuple. 
> 
> 
> 
> Observe:
> 
>     
> 
> py> class Foo(object):
> 
> 	pass
> 
> py> foo = Foo()
> 
> py> t = (1,'1', foo)
> 
> py> t
> 
> (1, '1', <__main__.Foo object at 0x0267BF50>)
> 
> py> t[-1].bar = "abc"
> 
> py> t
> 
> (1, '1', <__main__.Foo object at 0x0267BF50>)
> 
> 
> 
> Or by expanding a list
> 
> 
> 
> py> t = (1,2,3)
> 
> py> t = t+([],)
> 
> py> t
> 
> (1, 2, 3, [])
> 
> py> t[-1].append('circus')
> 
> py> t
> 
> (1, 2, 3, ['circus'])
> 
> 
> 
> [1] Which is an unfortunate side-effect of polysemy and compounded exponentially by naive (and sometimes a purely malevolent intent when) transformation of words into esoteric problem domains.




More information about the Python-list mailing list