Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Puzzled by "is"

Reply
Thread Tools

Re: Puzzled by "is"

 
 
=?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?=
Guest
Posts: n/a
 
      08-09-2007
>
> Why? Because.
>
> Seriously, it's just an optimization by the implementers. There is no
> need for more than one empty tuple, since tuples can never be modified
> once created.
>
> But they decided not to create (1, ) in advance. They probably knew that
> hardly anybody would want to create that tuple [Seriously: if you
> started trying to predict which tuples would be used you would go
> insane, but the empty tuple is the most likely candidate].
>

That's just theorisation but I'd rather expect the interpreter simply
not to create a second tuple while there already is an identical one.
This could save some memory if the tuple was large (Although by the same
token comparison of large tuples can be expensive). Admittedly the empty
tuple is a special case but then 'Special cases aren't special enough to
break the rules'.

A bit odd.

Best regards,
Greg
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      08-09-2007
Grzegorz Słodkowicz wrote:

> That's just theorisation but I'd rather expect the interpreter simply
> not to create a second tuple while there already is an identical one.
> This could save some memory if the tuple was large (Although by the same
> token comparison of large tuples can be expensive). Admittedly the empty
> tuple is a special case but then 'Special cases aren't special enough to
> break the rules'.
>
> A bit odd.


It doesn't save time if you have to check through all the existing
tuples for matches ...

--
Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Chance favors the trained mind.
-- Louis Pasteur
 
Reply With Quote
 
 
 
 
Hrvoje Niksic
Guest
Posts: n/a
 
      08-09-2007
Grzegorz Sodkowicz <(E-Mail Removed)> writes:

>> Seriously, it's just an optimization by the implementers. There is
>> no need for more than one empty tuple, since tuples can never be
>> modified once created.
>>
>> But they decided not to create (1, ) in advance. They probably knew
>> that hardly anybody would want to create that tuple [Seriously:
>> if you started trying to predict which tuples would be used you
>> would go insane, but the empty tuple is the most likely candidate].
>>

> That's just theorisation but I'd rather expect the interpreter simply
> not to create a second tuple while there already is an identical
> one.


But then tuple creation would be slowed down by searching for whether
an "identical one" already exists. In the general case, that is quite
unlikely, so it's not done. (I suspect that only the requirement to
store the list of all tuples somewhere would outweigh any potential
gains of this strategy; and if the search were implemented as a hash
table lookup, even more space would be wasted.) It's done for the
empty tuple because no search is necessary, only a size test.

> Admittedly the empty tuple is a special case but then 'Special cases
> aren't special enough to break the rules'.


Except no rule is being broken. As others have pointed out, since
tuples are immutable, caching them is quite safe.
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      08-10-2007
Grzegorz Słodkowicz <(E-Mail Removed)> writes:

> That's just theorisation but I'd rather expect the interpreter
> simply not to create a second tuple while there already is an
> identical one.


Others have already said that it's an implementation optimisation,
which seems to partly answer your question.

It's important to also realise that the language is *deliberately*
non-committal on whether any given value will have this behaviour;
that is, it's entirely left to the language implementation which
optimisation trade-offs to make, and the language user (that's you and
I) should *not* expect any particular behaviour to hold between
different implementations.

--
\ "Holy priceless collection of Etruscan snoods, Batman!" -- |
`\ Robin |
_o__) |
Ben Finney
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-10-2007
Ben Finney wrote:

> It's important to also realise that the language is *deliberately*
> non-committal on whether any given value will have this behaviour;
> that is, it's entirely left to the language implementation which
> optimisation trade-offs to make, and the language user (that's you and
> I) should *not* expect any particular behaviour to hold between
> different implementations.


Right. In the terminology of language standards, it is implementation
defined.

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Only love is worth the risk
-- Oleta Adams
 
Reply With Quote
 
Dick Moores
Guest
Posts: n/a
 
      08-10-2007
At 06:13 PM 8/9/2007, Ben Finney wrote:
>Content-Transfer-Encoding: base64Grzegorz
>Słodkowicz <(E-Mail Removed)ܚ]\΂] ȝ@st
>theorisation but I'd rather expect the interpreter
> > simply not to create a second tuple while there already is an
> > identical one.

>
>Others have already said that it's an implementation optimisation,
>which seems to partly answer your question.
>
>It's important to also realise that the language is *deliberately*
>non-committal on whether any given value will have this behaviour;
>that is, it's entirely left to the language implementation which
>optimisation trade-offs to make, and the language user (that's you and
>I) should *not* expect any particular behaviour to hold between
>different implementations.


I'm not clear on the meaning of "implementations"
here. Would 2.5 for Windows, Mac, Linux all be
different implementations? Would Iron Python be another? ActivePython?

Thanks,

Dick


 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-10-2007
On Fri, 10 Aug 2007 10:57:22 -0700, Dick Moores wrote:

> At 06:13 PM 8/9/2007, Ben Finney wrote:
>>Others have already said that it's an implementation optimisation,
>>which seems to partly answer your question.
>>
>>It's important to also realise that the language is *deliberately*
>>non-committal on whether any given value will have this behaviour;
>>that is, it's entirely left to the language implementation which
>>optimisation trade-offs to make, and the language user (that's you and
>>I) should *not* expect any particular behaviour to hold between
>>different implementations.

>
> I'm not clear on the meaning of "implementations"
> here. Would 2.5 for Windows, Mac, Linux all be
> different implementations? Would Iron Python be another? ActivePython?


Taken to the extreme you shouldn't rely on implementation details at all,
so even the very same interpreter might cache and reuse the empty tuple in
one run but not in the next.

For me CPython, Iron Python and Jython are different implementations as
they are all written from scratch.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Trent Mick
Guest
Posts: n/a
 
      08-10-2007
Dick Moores wrote:
> At 06:13 PM 8/9/2007, Ben Finney wrote:
>> It's important to also realise that the language is *deliberately*
>> non-committal on whether any given value will have this behaviour;
>> that is, it's entirely left to the language implementation which
>> optimisation trade-offs to make, and the language user (that's you and
>> I) should *not* expect any particular behaviour to hold between
>> different implementations.

>
> I'm not clear on the meaning of "implementations"
> here. Would 2.5 for Windows, Mac, Linux all be
> different implementations? Would Iron Python be another? ActivePython?


(Note that I'm jumping into this without having read the thread, so
apologies if I didn't grasp the question in its proper context.)

Separate Python "implementations" generally refers to a separate source
code base for that Python build. According to that definition, current
examples of Python implementations are: CPython (from python.org,
C-based), Jython (Java-based), Stackless Python (I'm not that familiar
to how much of the CPython source code, if any, Stackless borrows),
IronPython (.NET/C#-based), possibly PyPy (Python-based, I'm not that
familiar with it). Also eventually interesting might be the recently
announced IronMonkey (http://wiki.mozilla.org/Tamarin:IronMonkey).

Windows, Mac, Linux, etc. installations/builds/installers of a
particular implementation would be different platform *builds* (my
language).

ActivePython is a separate (from python.org's) *distribution* of CPython
-- i.e. the sample implementation as CPython (same source code). This is
similar, in some respects, to SuSE, Debian, RedHat, Ubuntu, etc. being
different distributions of Linux.


Trent

--
Trent Mick
trentm at activestate.com
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      08-12-2007
Dick Moores <(E-Mail Removed)> writes:

> At 06:13 PM 8/9/2007, Ben Finney wrote:
> >it's entirely left to the language implementation which
> >optimisation trade-offs to make, and the language user (that's you
> >and I) should *not* expect any particular behaviour to hold between
> >different implementations.

>
> I'm not clear on the meaning of "implementations" here. Would 2.5
> for Windows, Mac, Linux all be different implementations? Would Iron
> Python be another? ActivePython?


For the purpose of the above statement, you should consider even the
same Python on two different machines to be "different
implementations". As a programmer writing Python code, you should not
expect any "implementation-dependent" behaviour to operate in any
particular way.

--
\ "The fact that a believer is happier than a skeptic is no more |
`\ to the point than the fact that a drunken man is happier than a |
_o__) sober one." —George Bernard Shaw |
Ben Finney
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      08-12-2007
Dick Moores wrote:
> On 8/12/07, *Ben Finney* <(E-Mail Removed)
> <(E-Mail Removed)>> wrote:
>
> Dick Moores <(E-Mail Removed) <(E-Mail Removed)>> writes:
>
> > At 06:13 PM 8/9/2007, Ben Finney wrote:
> > >it's entirely left to the language implementation which
> > >optimisation trade-offs to make, and the language user (that's you
> > >and I) should *not* expect any particular behaviour to hold between
> > >different implementations.

> >
> > I'm not clear on the meaning of "implementations" here. Would 2.5
> > for Windows, Mac, Linux all be different implementations? Would Iron
> > Python be another? ActivePython?

>
> For the purpose of the above statement, you should consider even the
> same Python on two different machines to be "different
> implementations". As a programmer writing Python code, you should not
> expect any "implementation-dependent" behaviour to operate in any
> particular way.
>
>
> So would a programmer EVER use "is" in a script?


Sure. For example, the canonical test for None uses

x is None

because there is only ever one instance of type Nonetype, so it's the
fastest test. Generally speaking you use "is" to test for identity (do
these two expressions reference the same object) rather than equality
(do these two expressions evaluate to equivalent objects).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

 
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
ItemCommand event not firing from a dynamic user control,feeling puzzled EvelynAnd Ethan ASP .Net 4 01-09-2006 03:12 AM
Puzzled about viewstate error jeff29_b@yahoo.com ASP .Net 5 09-14-2005 12:29 AM
Very Puzzled, Please Help schiefaw ASP .Net 3 07-22-2005 07:03 PM
VHDL: puzzled beginner Mark Andrew VHDL 5 06-30-2004 04:06 AM
still puzzled Daniel ASP .Net 11 06-09-2004 07:16 AM



Advertisments