Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Roshan Mathews
On Wed, Oct 21, 2009 at 12:05 PM, Sidharth Kuruvila
sidharth.kuruv...@gmail.com wrote:
  d = {a:Hello}
  print d.setdefault(a, blah)

  Even though the string blah is not being used an object has to be
 created to represent it. Even worse, you could put some complex
 expression in there expecting it to evaluate only if the key is
 missing.

Oh, alright.

Roshan Mathews
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Anand Chitipothu
On Wed, Oct 21, 2009 at 12:05 PM, Sidharth Kuruvila
sidharth.kuruv...@gmail.com wrote:
 Hi,

  d = {a:Hello}

  print d.setdefault(a, blah)

  Even though the string blah is not being used an object has to be
 created to represent it. Even worse, you could put some complex
 expression in there expecting it to evaluate only if the key is
 missing.

Your explanation is correct for the case of expressions but not for
string blah.

Literal strings are interned. Python maintains a dict of all literal
strings used in the code and all occurrences get the same object.

 id(hello)
600320
 id(hello)
600320

But if it is an expression, different object is created every time.

 id(he + llo)
600704
 id(he + llo)
600768

Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Anand Chitipothu
 What do you mean when you use the word interned?

$ pydoc intern
Help on built-in function intern in module __builtin__:

intern(...)
intern(string) - string

``Intern'' the given string.  This enters the string in the (global)
table of interned strings whose purpose is to speed up dictionary lookups.
Return the string itself or the previously interned string object with the
same value.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Roshan Mathews
On Wed, Oct 21, 2009 at 12:31 PM, Anand Chitipothu anandol...@gmail.com wrote:
    ``Intern'' the given string.  This enters the string in the (global)
    table of interned strings whose purpose is to speed up dictionary lookups.
    Return the string itself or the previously interned string object with the
    same value.


Thanks, I didn't know of that.  It could be useful sometime.  Anyways,
for the current discussion intern-ing is irrelevant.

 id('superman')
30792544
 id('superman')
30792544
 id('superman')
30792544
 id('superman')
30792544
 id('superman')
30792544
 id('super man')
31955768
 id('super man')
31955488
 id('super man')
31956768
 id('super man')
31955768
 id('super man')
31955488

Also, http://mail.python.org/pipermail/tutor/2009-July/070157.html

Roshan Mathews
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Sidharth Kuruvila
Hi,
  My bad, that was a bit of laziness on my part. The reason why my
code was silly is not to do with interning though that does happen for
strings. Literals, that is numbers and string literals and a few
others are loaded as constants. So the cost of constructing them in
your code has already been taken care of.

A better example would be something like

d = {a:[1,2,3,4]}

print d.setdefault(a, [])


Interning is an optimization done to speed up the comparison of
strings, by making sure that two string with the same text are
represented by the same object.

Regards,
Sidharth


On Wed, Oct 21, 2009 at 12:31 PM, Anand Chitipothu anandol...@gmail.com wrote:
 What do you mean when you use the word interned?

 $ pydoc intern
 Help on built-in function intern in module __builtin__:

 intern(...)
    intern(string) - string

    ``Intern'' the given string.  This enters the string in the (global)
    table of interned strings whose purpose is to speed up dictionary lookups.
    Return the string itself or the previously interned string object with the
    same value.
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Lakshman Prasad
On a similar note, here's a question I recently asked and obtained good
input about tail recursion optimization.
http://stackoverflow.com/questions/1414581/python-recursive-program-to-prime-factorize-a-number

On Wed, Oct 21, 2009 at 1:14 PM, Sidharth Kuruvila 
sidharth.kuruv...@gmail.com wrote:

 Hi,
  My bad, that was a bit of laziness on my part. The reason why my
 code was silly is not to do with interning though that does happen for
 strings. Literals, that is numbers and string literals and a few
 others are loaded as constants. So the cost of constructing them in
 your code has already been taken care of.

 A better example would be something like

 d = {a:[1,2,3,4]}

 print d.setdefault(a, [])


 Interning is an optimization done to speed up the comparison of
 strings, by making sure that two string with the same text are
 represented by the same object.

 Regards,
 Sidharth


 On Wed, Oct 21, 2009 at 12:31 PM, Anand Chitipothu anandol...@gmail.com
 wrote:
  What do you mean when you use the word interned?
 
  $ pydoc intern
  Help on built-in function intern in module __builtin__:
 
  intern(...)
 intern(string) - string
 
 ``Intern'' the given string.  This enters the string in the (global)
 table of interned strings whose purpose is to speed up dictionary
 lookups.
 Return the string itself or the previously interned string object with
 the
 same value.
  ___
  BangPypers mailing list
  BangPypers@python.org
  http://mail.python.org/mailman/listinfo/bangpypers
 



 --
 I am but a man.
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
Regards,
Lakshman
becomingguru.com
lakshmanprasad.com
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


[BangPypers] An interesting beginner post at Stackoverflow

2009-10-20 Thread srid
http://stackoverflow.com/questions/1597764/is-there-a-better-pythonic-way-to-do-this

Someone wrote their *first* Python program asking for a more Pythonic
way to do it ... and gets valuable feedback from the community
including Alex Martelli.

I am now researching on a way to gather top posts (w/ python tag) on
Stackoverflow to create something similar to weeklyreddit.appspot.com
...

-srid
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-20 Thread Roshan Mathews
On Wed, Oct 21, 2009 at 8:06 AM, srid sridhar.ra...@gmail.com wrote:
 http://stackoverflow.com/questions/1597764/is-there-a-better-pythonic-way-to-do-this

Nice.  Martelli says:
(avoid setdefault, that was never a good design and doesn't have
 good performance either, as well as being pretty murky)

Any idea why?

 I am now researching on a way to gather top posts (w/ python tag) on
 Stackoverflow to create something similar to weeklyreddit.appspot.com

Weekly Reddit is such a cool idea, except I end up checking proggit
frequently anyways.  Then I guilt out when I get the rss posts on
Sunday.  :-S

Roshan Mathews
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers