OOP - Python - 当我单独调用静态方法时也打印实例变量

OOP - Python - printing instance variable too when I call static method alone

在此代码中,我只是调用了我的静态方法,但它也打印了我的实例变量。能否请您解释一下原因,以及如何避免它们被打印出来?

如下所示:

我是静态方法

None

class Player:
    
     def __init__(self, name = None):
        self.name = name # creating instance variables
         
     @staticmethod
     def demo():
         print("I am a static Method")
         
p1 = Player()

print(p1.demo())

作为Python docs says:

Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments.

所以你可以 return 你的消息在方法中,然后 print 它:

class Player:
    
     def __init__(self, name = None):
        self.name = name # creating instance variables
         
     @staticmethod
     def demo():
         return "I am a static Method"
         
p1 = Player()

print(p1.demo())