python - 如何在不需要自变量的情况下使对象可访问?
问题描述
例如在 discord.py 中:
import discord
discord.Status.dnd
^ 另一个例子:
class Test:
def __init__(self, word):
print(word)
这项工作需要 A = Test("blah") ,但是我如何使它像 Test("blah") 而不是给它变量?
解决方案
你可以这样做:
class Test:
word = ''
print(Test.word) #Will return ''
Test.word = 'Blah'
print(Test.word) #Will return 'Blah'
instance = Test()
print(instance.word) #Will return 'Blah'
instance.word = 'Wow'
print(instance.word) #Will return 'Wow'
print(Test.word) #Will still return 'Blah'
推荐阅读
- php - 在 Woocommerce 中显示当前子类别的子类别
- swift - 如何在 iOS swift 4 中访问 Magento Rest API
- laravel - laravel 通知未定义索引:user_id
- migration - Keycloak 迁移散列密码
- java - Selenide 可以与 SpecFlow 一起使用吗?
- angular - 从网络应用程序向 chrome 扩展发送消息
- java - 如何使用 Spring Boot Gradle Plugin 2.1.* 构建不可执行的 jar(库)
- mongodb - 如何根据给定的条件提升 Mongodb 搜索结果
- python - 如何在 elasticsearch-dsl 中加入两个 Document 对象?
- python - Django CustomUser 在管理员中创建,通过电子邮件通知用户