Python:如何在同一个文件中调用一个类

Nat*_*ume 2 python

如何在同一个文件中调用类.我的文件是这样的:

class one:
    def get(self):
        return 1

class two:
    def init(self):
        get class one get()
Run Code Online (Sandbox Code Playgroud)

如何获得一级get().对不起,我的英语不好.

NPE*_*NPE 6

One.get()如果将其转换为静态方法,则可以直接调用:

class One:
    @staticmethod
    def get():
        return 1

class Two:
    def __init__(self):
        val = One.get()
Run Code Online (Sandbox Code Playgroud)

没有@staticmethod,你需要一个实例,One以便能够调用get():

class One:
    def get(self):
        return 1

class Two:
    def __init__(self):
        one = One()
        val = one.get()
Run Code Online (Sandbox Code Playgroud)