So I have the selfmade class element and I want to create a function, that takes in keyword-arguments which get passed into the constructor of element
It looks like this:
class element:
def __init__(self, name="element", count=1):
self.name = name
self.count = count
def create_element(**kwargs):
try:
el = element(**kwargs)
except TypeError:
print("You chose arguments which no element has")
return el
My problem is not that I want a completely different code, but rather to fix the problem I have when I want to return el of the method create_element. If I run this code now, I have a UnboundLocalError, which tells me, that I am accessing a variable before initialing it.
I have tried a finally block, but it had the same result.
I see why this is but I don't have a solution.
defkeyword before your__init__That is, your code should saydef __init__(self, name = "element", count =1):