Python 如何在不需要自变量的情况下使对象可访问?

Python 如何在不需要自变量的情况下使对象可访问?,python,oop,object,discord.py,Python,Oop,Object,Discord.py,例如,在discord.py中: import discord discord.Status.dnd ^另一个例子: class Test: def __init__(self, word): print(word) 这项工作需要一个=Test(“blah”)来完成,但是我如何使它像Test(“blah”)一样,而不是给它变量呢?您可以这样做: 类测试: 单词=“” 打印(Test.word)#将返回“” Test.word='Blah' 打印(Test.word)#

例如,在discord.py中:

import discord
discord.Status.dnd
^另一个例子:

class Test:
    def __init__(self, word):
        print(word)

这项工作需要一个=Test(“blah”)来完成,但是我如何使它像Test(“blah”)一样,而不是给它变量呢?

您可以这样做:

类测试:
单词=“”
打印(Test.word)#将返回“”
Test.word='Blah'
打印(Test.word)#将返回“Blah”
实例=测试()
print(instance.word)#将返回'Blah'
instance.word='Wow'
print(instance.word)#将返回“哇”
打印(Test.word)#仍将返回“Blah”

如果这是你的问题的解决方案,请考虑接受它,如所说的。