I have a question about class in Python.

Asked 2 years ago, Updated 2 years ago, 19 views

class Test :

def foo1(self) :
    print(id(self))

def foo2() :
    print('foo4')

TestClass = Test()
TestClass.foo1()

Test.foo2()

 """ =================================== """

TestClass.foo2()
Test.foo1()

If you write it like this, the code below will be error based on the perforation What is the difference between the code above and the code below based on the perforation?

python

2022-09-22 20:45

1 Answers

First of all, you can't call the class right away, so you have to create a TestClass instance that specifies the TestClass like the code above.

Test.foo1() //Working

TestClass = Test()
TestClass.foo1() // Operated

In the class, the function needs to be self as the first factor to operate.p

So you have to fix the foo2() function like foo2(self)

This is a site where you can learn more about Python classes. It will be helpful if you read it

https://wikidocs.net/28


2022-09-22 20:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.