I am learning OOP with python.
I want to implement something like to display:
firstName, lastName of a person with address as dictionary like, 'state': someState, 'city': someCity
I’ve done:
class data:
def __init__(self, firstName, lastName, **address):
self.firstName = firstName
self.lastName = lastName
self.address = {
"state": self.state,
"city" : self.city
}
When I did:
>>> d = data("a", "b", 'state' : "stat", 'city' : "ci")
SyntaxError: invalid syntax
>>> d = data("a", "b", 'state'="stat", 'city'="ci")
SyntaxError: keyword can't be an expression
Is there mistake with my code or the syntax of accessing dictionary is mistake. I can not figure it out.
You should just define a new dict as the argument to your constructor and then copy it in the constructor like this.