<!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">
Peter Otten wrote:
<blockquote cite="mid:%3Chqkjbu$l9p$02$1@news.t-online.com%3E"
 type="cite">
  <pre wrap="">Alan Harris-Reid 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 = 1
class1.attr2 = 2
class1.attr3 = 3
class1.attr4 = 4
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 = 1
   .attr2 = 2
   .attr3 = 3
   .attr4 = 4
   etc.
endwith

Is there any equivalent to this in Python?
    </pre>
  </blockquote>
  <pre wrap=""><!---->
No. You could write a helper function

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">def update(obj, **kw):
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->...     for k, v in kw.items():
...             setattr(obj, k, v)
...

and then use keyword arguments:

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">class A: pass
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->...
  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">a = A()
update(a, foo=42, bar="yadda")
a.foo, a.bar
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->(42, 'yadda')
  </pre>
  <pre wrap=""><!---->
But if you are doing that a lot and if the attributes are as uniform as 
their names suggest you should rather use a Python dict than a custom class.

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">d = {}
d.update(foo=42, bar="whatever")
d
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->{'foo': 42, 'bar': 'whatever'}
  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">d["bar"]
        </pre>
      </blockquote>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->'whatever'

Peter</pre>
</blockquote>
Hi Peter, thanks for the reply,<br>
<br>
Interesting solution, but it looks as though it may be easier to repeat
the class prefix a few times.<br>
<br>
Alan<br>
<br>
</body>
</html>