1

I always liked how VB.NET allows you to take a series of statements with long variable names and make them more readable using the 'With' Statement (you can see an example in "The VB.NET 'With' Statement - embrace or avoid?"). It can really make the code much more readable.

So is there is an equivalent keyword or construct to do the same thing in Python?

Big_Al_Tx
  • 954
  • 9
  • 14

2 Answers2

2

No, but you could do something like this:

obj.__dict__.update({
    'a': 1,
    'b': 2,
    'c': 3,
})

However, working with __dict__ directly is not advised.

Ben
  • 2,422
  • 2
  • 16
  • 23
1

Long accessor chains are a code smell. If you find that you're manipulating several attributes of an object gotten via an accessor chain it's time to write a method for it. It's also bad style to grab and manipulate "private parts" of objects without their consent...

Your question might better be phrased as "What is a proper pythonic way to set several attributes of a contained object?" together with an actual real-life example which allows answers to be specific.