社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  cdraper  »  全部回复
回复总数  1
4 年前
回复了 cdraper 创建的主题 » 如何在Python中导入类的实例?

只要你初始化你的类实例 Tweeter

# tweeter.py
Class Tweeter:
    ...


# This code is ran whenever this file is imported from another file
tweeter = Tweeter(<arguments to pass to the __init__ function>)



# my_file.py


from .tweeter import tweeter # Assuming both files are in the same directory

# now you have the same class instance from the tweeter.py file
print(tweeter)