Pablo Pablo - 1 year ago 93
Python Question

Instance variable name a reserved word in Python

I checked the Python style guide, and I found no specific references to having instance variable names with reserved words e.g.

, etc.

What's the best practice for this?

Answer Source

Avoid it if possible.

You can get and set such attributes via getattr and setattr, but they can't be accessed with ordinary dot syntax (something like obj.class is a syntax error), so they're a pain to use.

As Aurora0001 mentioned in a comment, a convention if you "need" to use them is to append an underscore. The most common reason to "need" to have such attributes is that they're generated programatically from an external data source.

(Note that type is not a keyword, so you can do self.type just fine.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download