-1

I was writing code and came across a certain anomaly in my opinion. I created a primer that reproduces this error.

num2 = 1234
num1 = 123
class.class1():
    def function1(num1, num2):
        t = num1 + num2
        return t

  
    def function2(num1, num2):
        print(function1(num1, num2))


class1.function2(num1, num2)

console

Traceback (most recent call last): File "/home/runner/Python/main.py", line 12, in roro.function2(num1, num2) File "/home/runner/Python/main.py", line 9, in function2 print(function1(num1, num2)) NameError: name 'function1' is not defined

I didn’t find the answer on the Internet and asked the AI in "replit" to solve this problem, but it gave me a “corrected code” that was identical to mine. In general, I'm at a dead end. Please help. Tell me what am I doing wrong?

I added self to every function but it didn't work.

and I did this: "The object the function is called on automatically gets passed as self.

So if you call x.add(y), self will be x and other will be y"

class

Traceback (most recent call last): File "/home/runner/Python/main.py", line 11, in class1.num1.function2(num2) AttributeError: type object 'class1' has no attribute 'num1'

Traceback (most recent call last): File "/home/runner/Python/main.py", line 11, in class1.function2(num1, num2) TypeError: class1.function2() missing 1 required positional argument: 'num2'

Traceback (most recent call last): File "/home/runner/Python/main.py", line 11, in class1.function2(num1, num2) TypeError: class1.function2() missing 1 required positional argument: 'num2'

1

1 Answer 1

1

There are a few problems with your code. First, the class declaration:

class.class1():

Should be:

class class1:

And you should use self to reference the class member funtions. The final code should be:

num2 = 1234
num1 = 123

class class1:
    def function1(self, num1, num2):
        t = num1 + num2
        return t


    def function2(self, num1, num2):
        print(self.function1(num1, num2))

c1 = class1()
c1.function2(num1, num2)

I've tested it and it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Traceback (most recent call last): File "/home/runner/Python/main.py", line 12, in <module> c1.function2(num1, num2) NameError: name 'c1' is not defined

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.