<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Iain King wrote:
<blockquote
 cite="mid:%3C4fba6bea-49b0-47b6-8a72-b700c65b3033@c27g2000vbb.googlegroups.com%3E"
 type="cite">
  <pre wrap="">On Apr 20, 2:43 pm, Alan Harris-Reid <a class="moz-txt-link-rfc2396E" href="mailto:aharrisr...@googlemail.com"><aharrisr...@googlemail.com></a>
wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Hi,

During my Python (3.1) programming I often find myself having to repeat
code such as...

class1.attr1 =
class1.attr2 =
class1.attr3 =
class1.attr4 =
etc.

Is there any way to achieve the same result without having to repeat the
class1 prefix?  Before Python my previous main language was Visual
Foxpro, which had the syntax...

with class1
   .attr1 =
   .attr2 =
   .attr3 =
   .attr4 =
   etc.
endwith

Is there any equivalent to this in Python?

Any help would be appreciated.

Alan Harris-Reid
    </pre>
  </blockquote>
  <pre wrap=""><!---->
The pythonic equivalent of VB 'with' is to assign to a short variable
name, for example '_':

_ =lass1
_.attr1 =
_.attr2 =
_.attr3 =
_.attr4 =

alternatively, you could use the __setattr__ method:

for attr, value in (
    ('attr1', 1),
    ('attr2', 2),
    ('attr3', 3),
    ('attr4', 4)):
    class1.__setattr__(attr, value)

and to get a bit crunchy, with this your specific example can be
written:

for i in xrange(1, 5):
    class1.__setattr__('attr%d' % i, i)

Iain</pre>
</blockquote>
Hi Iain, thanks for the reply,<br>
<br>
Like the _. prefix idea - I didn't know _ could be used as a variable
name.<br>
<br>
<pre wrap="">>for i in xrange(1, 5):
>    class1.__setattr__('attr%d' % i, i)

</pre>
Good solution if the values matched the attribute names, but
unfortunately they don't.  That was just a (bad) example to illustrate
my problem.<br>
<br>
Regards,<br>
Alan<br>
<br>
<br>
<br>
<br>
</body>
</html>