I have to make class objects with dictionaries inside. I can't figure out how to change values when making them. I can change them after, but same code doesn't work inside making line.
There are some attempts (#tells error from code below, i did't change anything other than machine1 making code):
#on this i get error: keyword cant be expression
class One():
def __init__(self, problem):
self.problem = {
"year": 0,
"model": 0,
"stupidity": 0
}
machine1 = One(problem[year]=1)
#TypeError: __init__() takes 2 positional arguments but 4 were given
class One():
def __init__(self, problem):
self.problem = {
"year": 0,
"model": 0,
"stupidy": 0
}
machine1 = One(1,1,1)
#does't change anything
class One():
def __init__(self, problem):
self.problem = {
"year": 0,
"model": 0,
"stupidy": 0
}
machine1 = One(1)
print(machine1.problem["year"])
#I can change it later with this
machine1.problem["year"] = 1
machine1 = One(1)seems to be intended to set the"year"key to1. Why should your__init__()method know that it is the"year"key that should be set and not the other keys?dict, and it looks like the call site:machine1 = One(problem[year]=1)is an attempt to set one of the keys. In my head, in the general case, that is a merge of two dicts. See the answer from Ajax1234: stackoverflow.com/a/55985254