Re: assign class variable in __init__

2010-06-08 Thread danieldelay

you can also use :

>>> class A(object):
def __init__(self, **args):
self.__dict__.update(args)

>>> a=A(source='test', length=2)
>>> a.source
'test'

but this is to be used carefully because mispelling your args somewhere 
in your program will not raise any error :

>>> A(sourc='test', lenth=2)

Daniel
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError: local variable referenced before assignment

2010-06-08 Thread danieldelay

Le 08/06/2010 10:03, ch1zra a écrit :

import os, time, re, pyodbc, Image, sys
from datetime import datetime, date, time
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import mkTable

mkTable.mkTable()

and then file mkTable.py located in same directory has :

def mkTable():
 global canvas
 canvas = canvas.Canvas(fname, pagesize=A4)
 ... and so on


Hello,

It does not really make sense to redefine an object you've imported, 
like in

> import mkTable
> def mkTable():

or like in :
> from reportlab.pdfgen import canvas
> canvas = ...

You must use differents names for the variables you wan't to use, or the 
function you wan't to import


> from reportlab.pdfgen import canvas
> 
> import mkTable
> ...
> def MYmkTable():
>  ...
>  MYcanvas = canvas.Canvas(fname, pagesize=A4)

Daniel
--
http://mail.python.org/mailman/listinfo/python-list


why any( ) instead of firsttrue( ) ?

2010-06-08 Thread danieldelay

Hi,

I find very useful in python the ability to use a list or number x like 
a boolean :


   if x :
  do something


So I don't understand why was introduced the any( ) function defined as :

  def any(iterable):
for element in iterable:
if element:
return True
return False

instead of a function firsttrue( ) that could have been defined as :

  def firsttrue(iterable):
for element in iterable:
if element:
return element
return None

This function "firsttrue( )" could probably be used anywhere "any( )" is 
used, but with the ability to retrieve the first element where 
bool(element) is True, which may be sometimes usefull.


I suppose that there is a reason for that, does anybody know it ?

Daniel
--
http://mail.python.org/mailman/listinfo/python-list


Re: why any( ) instead of firsttrue( ) ?

2010-06-08 Thread danieldelay

Le 09/06/2010 00:24, Ian Kelly a écrit :

Because it was designed as a replacement for "reduce(lambda x, y: x or
y, iterable)".  The problem arises when the iterable is empty.  What
false value should be returned?  If the iterable is a sequence of
bools, then None doesn't fit.  If the iterable is a sequence of
non-bool objects, then False doesn't fit.  In the case of reduce, the
problem is solved by explicitly specifying an initial value to be used
when the sequence is empty, but I guess GVR didn't feel that was
appropriate here.

Cheers,
Ian


Thanks for your reply, it helps me to understand this choice wether I do 
not agree with it.


"False" sounds natural for a function called "any()" which makes a 
boolean calculus


"None" sounds natural for a function called "firsttrue()" which tries to 
retrieve an element.


As the two make sense, I would have chosen the "firsttrue()" which is 
more powerfull...


Perhaps "any()" whas choosen to keep a beautiful symmetry with "all()", 
that I can interprete only as a boolean calculus.


Does GVR prefers beauty to power ?

firsttrue(line.strip() for line in '\n\n \n CHEERS  \n'.split('\n'))

Daniel.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why any( ) instead of firsttrue( ) ?

2010-06-09 Thread danieldelay

Le 09/06/2010 08:54, Raymond Hettinger a écrit :

next(ifilter(None, d), False)


Good, this is rather short and does the job !...
I should try to use more often this  itertools  module.

Thanks

Daniel
--
http://mail.python.org/mailman/listinfo/python-list