[Chicago] April Talk Topic Python 3.2 Features

J. D. Jordan zanson at zanson.org
Tue Mar 13 03:27:29 CET 2012


For the format thing, the {} has 0, because you are formatting
argument 0.  You could also make it table['Jack'] instead of 0['Jack']
and have .format(table=table)

nonlocal means that it is scoped outside of the current scope, but not
all the way up in global...

def bob(myvar):
  def subfunc():
    nonlocal myvar
    return myvar * 10
  return subfunc


On Fri, Mar 9, 2012 at 8:56 AM, Brian Ray <brianhray at gmail.com> wrote:
> Does anybody want to present on this at our April meeting (at Groupon.com)?
> Is this a good topic (can I get a +1)? If I can not convince someone else on
> presenting I am sure I can probably do something for 20-30 minutes.
>
> I had some food for thought (my example code and notes) while reviewing
> 3.2.  I am not a 3 expert at all and have been stuck in the 2.X world for
> ages.
>
> I found: 2 things I like and 2 things I do not quite understand (my advance
> apologies if your email reader does not support html):
>
> Like 1) concurrent.futures seems pretty neat. However I had to realize (doh)
> how exceptions are caught and be sure to get those with future.exceptions().
> See my commented out 1/0 below to raise an exception in the body of a future
> *thing*
>
> import concurrent.futures
> import time
> import random
>
> def _(): time.sleep(random.randint(0,9)) # ; 1/0
>
> pool_size = random.randint(5,15)
> print('printing {s} items finding'
>       ' squares between 1 and {s}'.format(s=pool_size))
>
> # Callback when each is done
> def cb(future):
>     if future.exception() is not None:
>         print('generated an exception: %s' % future.exception())
>     else:
>         print(future.result())
>
> # A simple runner based on pool_size
> with concurrent.futures.ThreadPoolExecutor(max_workers=pool_size) as e:
>     for i in range(pool_size):
>         i += 1
>         e.submit(lambda x: _() or (x,x**2), i).add_done_callback(cb)
>
>
> Like 2) Caching with functools lru_cache
>
>
> import functools
>
>
> @functools.lru_cache(maxsize=300)
> def function_that_does_heavy_lifting(same_thing):
>     print( "should only print once for {}".format(same_thing) )
>     return same_thing
>
> print(function_that_does_heavy_lifting("beer"))
> print(function_that_does_heavy_lifting("beer"))
> print(function_that_does_heavy_lifting("juice"))
> print(function_that_does_heavy_lifting("beer"))
> print(function_that_does_heavy_lifting("juice"))
> print(function_that_does_heavy_lifting("juice"))
>
>
> should only print once for beer
> beer
> beer
> should only print once for juice
> juice
> beer
> juice
> juice
>
> Question 1)  Why does {} have 0?
>
>
> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
>            'Dcab: {0[Dcab]:d}'.format(table))
>
> Jack: 4098; Sjoerd: 4127; Dcab: 8637678
>
> Question 2) what is the difference between nonlocal and global?
>
> def scope_test():
>     def do_local():
>         spam = "local spam"
>     def do_nonlocal():
>         nonlocal spam
>         spam = "nonlocal spam"
>     def do_global():
>         global spam
>         spam = "global spam"
>
>     spam = "test spam"
>     do_local()
>     print("After local assignment:", spam)
>     do_nonlocal()
>     print("After nonlocal assignment:", spam)
>     do_global()
>     print("After global assignment:", spam)
>
> scope_test()
> print("In global scope:", spam)
>
> FYI this says """The global statement can be used to indicate that
> particular variables live in the global scope and should be rebound there;
> the nonlocal statement indicates that particular variables live in an
> enclosing scope and should be rebound there.""" However, I do not understand
> yet fully what that means.
>
>
> Cheers, Brian
>
> --
> Brian Ray
> @brianray
> (773) 669-7717
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list