Implicit conversion to boolean in if and while statements

8

88888 Dihedral

Rick Johnsonæ–¼ 2013å¹´2月13日星期三UTC+8上åˆ1時48分07秒寫é“:
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 totuples, 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 theimmutable 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.
 
S

Serhiy Storchaka

Unfortunately, Python has a minor design flaw. One of the most common
use-cases for sets is for membership testing of literal sets:

def example(arg):
if arg in {'spam', 'ham', 'eggs', 'cheese'}:
...

Unfortunately, set literals create *mutable* sets, not frozensets. That
means that the compiler wastes time and memory creating am over-allocated
mutable set object. If set literals created immutable frozensets, there
would be some nice opportunities for the compiler to optimize this
use-case.

CPython 3.2 optimizes this special case and creates a constant frozenset.

$ echo "arg in {'spam', 'ham', 'eggs', 'cheese'}" | python3.2 -m dis -
1 0 LOAD_NAME 0 (arg)
3 LOAD_CONST 5 (frozenset({'cheese', 'eggs', 'ham', 'spam'}))
6 COMPARE_OP 6 (in)
9 POP_TOP
10 LOAD_CONST 4 (None)
13 RETURN_VALUE
 
I

Ian Kelly

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.

FYI, the general consensus here is that "he" (88888 Dihedral) is a
bot. It posts in non sequiturs and fails to respond meaningfully to
direct queries. A few months ago I posed an obvious Turing test for
it, which it failed.

http://thread.gmane.org/gmane.comp.python.general/718489

Your naive effort to extort meaning from its nonsense is admirable, however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top