Re: [Tutor] try except block for multiple statements

2008-12-07 Thread Kent Johnson
On Sun, Dec 7, 2008 at 12:25 PM, Lie Ryan <[EMAIL PROTECTED]> wrote:

> texts = ["a = %s\n" % plan.a,
> "b = %s\n" % plan.b,
> "c = %s\n" % plan.c,
> "d = %s\n" % plan.d
>]
>
> for text in texts:
>try:
>fo.write(text)
>except AttributeError:
>pass

No, the AttributeError will come during the creation of texts, not in the loop.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] try except block for multiple statements

2008-12-07 Thread Lie Ryan
On Mon, 01 Dec 2008 20:44:20 -0500, Bryan Fodness wrote:

> I would like to use a try except to see if a value exists.  But, when I
> use the following, if a does not exist it exits.  I understand why this
> does this, but is there a way to get b,c, and d if a does not exist
> without using a try except for every statement?
> 
> try:
> fo.write("a = %s\n" %plan.a)
> fo.write("b = %s\n" %plan.b)
> fo.write("c = %s\n" %plan.c)
> fo.write("d = %s\n" %plan.d)
> except AttributeError:
> pass

Or:
texts = ["a = %s\n" % plan.a, 
 "b = %s\n" % plan.b, 
 "c = %s\n" % plan.c, 
 "d = %s\n" % plan.d
]

for text in texts:
try:
fo.write(text)
except AttributeError:
pass

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] try except block for multiple statements

2008-12-01 Thread bob gailer

Bryan Fodness wrote:
I would like to use a try except to see if a value exists.  But, when 
I use the following, if a does not exist it exits.  I understand why 
this does this, but is there a way to get b,c, and d if a does not 
exist without using a try except for every statement?
 
try:

fo.write("a = %s\n" %plan.a)
fo.write("b = %s\n" %plan.b)
fo.write("c = %s\n" %plan.c)
fo.write("d = %s\n" %plan.d)
except AttributeError:
pass


def foo(obj, attr):
 val = getattr(obj, attr, None)
 if val is not None:
   obj.write("%s = %s\n" % (attr, val))
foo(plan, "a")
foo(plan, "b")
foo(plan, "c")
foo(plan, "d")



--
"Any intelligent fool can make things bigger, more complex, and more 
violent. It takes a touch of genius - and a lot of courage - to move 
in the opposite direction. "  -Albert Einstein



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  



--
Bob Gailer
Chapel Hill NC 
919-636-4239


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] try except block for multiple statements

2008-12-01 Thread John Fouhy
On 02/12/2008, Bryan Fodness <[EMAIL PROTECTED]> wrote:
> I would like to use a try except to see if a value exists.  But, when I use
> the following, if a does not exist it exits.  I understand why this does
> this, but is there a way to get b,c, and d if a does not exist without using
> a try except for every statement?
>
> try:
> fo.write("a = %s\n" %plan.a)
> fo.write("b = %s\n" %plan.b)
> fo.write("c = %s\n" %plan.c)
> fo.write("d = %s\n" %plan.d)
> except AttributeError:
> pass

AFAIK, no -- but you could always use a loop.

attrs = ['a', 'b', 'c', 'd']
for attr in attrs:
  try:
fo.write('A = %s\n' % getattr(plan, attr))
  except AttributeError:
pass

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] try except block for multiple statements

2008-12-01 Thread Bryan Fodness
I would like to use a try except to see if a value exists.  But, when I use
the following, if a does not exist it exits.  I understand why this does
this, but is there a way to get b,c, and d if a does not exist without using
a try except for every statement?

try:
fo.write("a = %s\n" %plan.a)
fo.write("b = %s\n" %plan.b)
fo.write("c = %s\n" %plan.c)
fo.write("d = %s\n" %plan.d)
except AttributeError:
pass

-- 
"Any intelligent fool can make things bigger, more complex, and more
violent. It takes a touch of genius - and a lot of courage - to move in the
opposite direction. "  -Albert Einstein
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor