0

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.

2
  • You're missing the defkeyword before your __init__ That is, your code should say def __init__(self, name = "element", count =1): Commented Jun 17, 2019 at 14:56
  • @Toothpick Anemone thanks, just a typing mistake... I will edit it. Commented Jun 17, 2019 at 14:57

3 Answers 3

2

you can define el with a default value outside the try:

class element:
    def __init__(self, name="element", count=1):
        self.name = name
        self.count = count

def create_element(**kwargs):
    el = None
    try:
        el = element(**kwargs)
    except TypeError:
        print("You chose arguments which no element has")
    finally:
        return el
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah but what do I do when I want some other Code outside the try-block which doesn't have to be tried? Of course I thought about that but I wanted an other answer, sorry
Thanks, thats the answer I was looking for! I feel so dumb that I couldn't see this on my own, it's so obvious!
0

You can initialize the return value to some default value before executing the try block:

el = None # initialize return value to default "None"
try:
    el = element(**kwargs)
except TypeError:
    print("You chose arguments which no element has")
return el

Alternatively, move the return statement inside of the try-block

try:
    el = element(**kwargs)
    return el
except TypeError:
    print("You chose arguments which no element has")

Comments

-1

This is why finally exists in python.

def create_element(**kwargs):
    el = None
    try:
        el = element(**kwargs)
    except TypeError:
        print("You chose arguments which no element has")
    finally:
        return el

2 Comments

here you'd still get UnboundLocalError: local variable 'el' referenced before assignment
If a type error occurs, then el never gets assigned to. el does not exist. Therefore, the finally block will raise an exception. You can't return el because there is no such thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.