关于错误TypeError: unbound method view_user() must be called with New_Login instance as first argument (got nothing instead):
class C:
… def f(self):
… print “hi”
…
*>>> C.f()
Traceback (most recent call last):
File “”, line 1, in
TypeError: unbound method f() must be called with C instance as
first argument (got nothing instead)
It fails because of TypeError because you didn’t instantiate the class first, you have two choices: 1: either make the method static so you can run it in a static way, or 2: instantiate your class so you have an instance to grab onto, to run the method.
如果你只是想运行静态的function。如下文:
class C:
… @staticmethod
… def f():
… print “hi”
…C.f()
hi
或者你想以被实例化的方法运行, 另一个文件需要import,同一个文件也需要把类实例化:如下
class C:
… def f(self):
… print “hi”
…c1 = C()
c1.f()
hiC().f()
hi
如果不知道什么情况用, 问自己几个问题。
- 静态方法和普通方法的表现有何不同。
- 实例化一个类是什么意思
- 静态方法和普通方法运行方式有何不同
- 类和对象的区别是什么