Reputation: 2090
Python 3, how do I reference a key named 'class':
print("id: " + eachasset.id)
print("name: " + eachasset.SName)
print("status: " + eachasset.status)
print("class: " + eachasset.class) # <-- gives me error: Invalid Syntax
Spyder 4 highlights the word class as purple.
Upvotes: 0
Views: 319
Reputation: 95948
.something
isn't a key, it is an attribute. You cannot use a key-word, like class
in an expression some_object.some_attribute
since key-words are reserved.
This was likely set dynamically, so your only option is to retrieve it dynamically, so:
getattr(eachasset, 'class')
Or just change the name to class_
, as you would conventionally do to name attributes and avoid using a keyword.
Upvotes: 3