Location of Python Functions and Variables

Asked 2 years ago, Updated 2 years ago, 73 views

class A:
    def aaa(self):
        return None

def aa():
    a.aaa()

a = A()

print(aa())
a = A()

class A:
    def aaa(self):
        return None

def aa():
    a.aaa()

print(aa())

When I use a function or class in Python, can I create a function even if the variable is not declared...? So it feels like you don't have to write a function or class at the top of the code in advance? I'm sorry. I was rambling. The above two codes work normally.

For example, the code below makes an error saying that the variable b has not been declared,,

class A:
    def aaa(self):
        return None

def aa():
    b.aaa()

a = A()

print(aa())

I can't find any information about it even if I google it, so I'm posting a question.

python function variable

2022-09-22 18:33

1 Answers

def aa(b):
    b.aaa()

a = A()

print(aa(a))


2022-09-22 18:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.