Short confusing example with unicode, print, and __str__

2008-03-05 Thread Gerard Brunick
I really don't understand the following behavior: class C(object): ... def __init__(self, s): self.s = s ... def __str__(self): return self.s ... cafe = unicode(Caf\xe9, Latin-1) c = C(cafe) print Print using c.s:, c.s Print using c.s: Café print Print using just c:, c Print

Re: Short confusing example with unicode, print, and __str__

2008-03-05 Thread Gerard Brunick
Gary Herron wrote: Gerard Brunick wrote: I really don't understand the following behavior: class C(object): ... def __init__(self, s): self.s = s ... def __str__(self): return self.s ... cafe = unicode(Caf\xe9, Latin-1) c = C(cafe) print Print using c.s:, c.s Print using

Can you escape a % in string that will used for substitution

2007-10-18 Thread Gerard Brunick
Is there a way to do: s = I like python %i%s of the time. print s % (99, %) without having to pass in %? Thanks, Gerard -- http://mail.python.org/mailman/listinfo/python-list

Adding functions to classes after definition

2007-01-08 Thread Gerard Brunick
Consider: A) class C(object): ... pass ... def f(*args): ... print args ... C.f = f C.f unbound method C.f c=C() c.f() (__main__.C object at 0x04A51170,) And B) del c C.f = types.MethodType(f, None, C) C.f unbound method C.f c = C() c.f() (__main__.C object at

Re: problem with closures

2006-12-07 Thread Gerard Brunick
I can't solve your problem, but I can at least explain why I think its hard. foo doesn't have any closed over variables. Some of its locals have to live in cells, so that pre and post can see them in their closures. foo.func_code.co_cellvars ('x', 'y') Now the only way that I know of to

Where does a class closure live?

2006-12-06 Thread Gerard Brunick
Consider: ### Function closure example def outer(s): ... def inner(): ... print s ... return inner ... f = outer(5) f() 5 dir(f) ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__',

Can you fix up wrapper function argument signatures?

2006-11-27 Thread Gerard Brunick
Consider: def negate(func): ... def wrapper(*args, **kwargs): ... return not func(*args, **kwargs) ... return wrapper ... def f(x): ... return x 10 ... g = negate(f) g(20) False g(5) True Now g has the argument signature of (*args, **kwargs). Pop-up help in

What is the slickest way to transpose a square list of lists (tuple of tuples)?

2006-01-08 Thread Gerard Brunick
My way is ugly. These has to be a better way. Thanks, Gerard -- http://mail.python.org/mailman/listinfo/python-list