I wrote the following Python code for illustration:
class foo:
def foo_function(self):
return 1
class bar:
def bar_function(self):
foo = foo()
return foo.foo_function()
bar = bar()
print(bar.bar_function())
where in class bar
I need to use class foo
. I got the following error when I run it:
# UnboundLocalError: local variable 'foo' referenced before assignment
This error comes from the line foo = foo()
where the variable and class have the same name,
and it will go away if use different names, but I would like to know what happened internally,
please note bar = bar()
is okay.
By the way, is this the right way to use one class from another class. I use separate class because they are independent: for example, foo
is to get secret stored on the AWS, and bar
uses the secret in a different app not related to the AWS at all.