Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Implicit conversion to boolean in if and while statements

Reply
Thread Tools

Implicit conversion to boolean in if and while statements

 
 
88888 Dihedral
Guest
Posts: n/a
 
      02-13-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 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.


 
Reply With Quote
 
 
 
 
Serhiy Storchaka
Guest
Posts: n/a
 
      02-15-2013
On 10.02.13 13:37, Steven D'Aprano wrote:
> 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


 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      02-15-2013
On Tue, Feb 12, 2013 at 10:48 AM, Rick Johnson
<(E-Mail Removed)> wrote:
> 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.


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.p...general/718489

Your naive effort to extort meaning from its nonsense is admirable, however.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-16-2013
On Sat, Feb 16, 2013 at 7:12 AM, Ian Kelly <(E-Mail Removed)> wrote:
> 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.p...general/718489


Which, I might add, was something awesome. I find Dihedral pretty
amusing, tbh... "permanently mutated list" is an interesting concept.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Implicit conversion to boolean in if and while statements Stefan Behnel Python 0 07-15-2012 09:17 AM
Re: Implicit conversion to boolean in if and while statements Chris Angelico Python 0 07-15-2012 08:47 AM
Subtle difference between boolean value and boolean comparison? Metre Meter Javascript 7 08-06-2010 08:40 PM
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM



Advertisments