首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python访问错误类的属性

Python访问错误类的属性
EN

Stack Overflow用户
提问于 2018-07-14 02:17:59
回答 1查看 279关注 0票数 1

我目前面临的问题是,我的Python脚本正在访问错误类的属性。这显然与我的项目中的一些名称空间冲突有关。

让我向您展示我当前的实现:

项目结构(所有内容都在同一个包中):

代码语言:javascript
运行
复制
|- bot_test_project    
   |- bot.py
   |- message.py

bot.py:

代码语言:javascript
运行
复制
import logging

from telegram.ext import Updater, MessageHandler, Filters, CommandHandler

API_KEY = "please enter your api key here"

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__)

class Bot:
    def __init__(self):
        updater = Updater(API_KEY)
        self.__bot = updater.bot

        dispatcher = updater.dispatcher
        dispatcher.add_handler(CommandHandler("start", self.receive_command, pass_user_data=True))
        dispatcher.add_handler(MessageHandler(Filters.text, self.receive_message, pass_user_data=True))

        updater.start_polling()

    def receive_command(self, bot, update, user_data):
        print(update.message.id)

    def receive_message(self, bot, update, user_data):
        print(update.message.id)


Bot()

message.py:

代码语言:javascript
运行
复制
class Message:
    def __init__(self, id=None, text=None, photo=None):
        self.__id = id
        self.__text = text
        self.__photo = photo

    @property
    def id(self):
        return self.__id

    @property
    def text(self):
        return self.__text

    @property
    def photo(self):
        return self.__photo

但是,我目前收到以下错误消息:

代码语言:javascript
运行
复制
2018-07-13 23:13:35,915 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telegram/ext/commandhandler.py", line 173, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "/bot.py", line 23, in receive_command
    print(update.message.id)
AttributeError: 'Message' object has no attribute 'id'

receive_message()实际上是第三方库(telegram-bot)的回调函数。因此,所有的函数参数都与第三方库类相关。

由于某些原因,Python在我的本地类中寻找text属性,但它应该访问更新的类属性。我甚至还没有导入我的本地类message

我当前使用的是Python 3.6

编辑:

代码语言:javascript
运行
复制
print(type(update.message))
<class 'telegram.message.Message'>

print(dir(update.message))
['ATTACHMENT_TYPES', 'MESSAGE_TYPES', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_effective_attachment', '_id_attrs', '_parse_html', '_parse_markdown', '_quote', 'audio', 'author_signature', 'bot', 'caption', 'caption_entities', 'caption_html', 'caption_html_urled', 'caption_markdown', 'caption_markdown_urled', 'channel_chat_created', 'chat', 'chat_id', 'connected_website', 'contact', 'date', 'de_json', 'delete', 'delete_chat_photo', 'document', 'edit_caption', 'edit_date', 'edit_reply_markup', 'edit_text', 'effective_attachment', 'entities', 'forward', 'forward_date', 'forward_from', 'forward_from_chat', 'forward_from_message_id', 'forward_signature', 'from_user', 'game', 'group_chat_created', 'invoice', 'left_chat_member', 'location', 'media_group_id', 'message_id', 'migrate_from_chat_id', 'migrate_to_chat_id', 'new_chat_members', 'new_chat_photo', 'new_chat_title', 'parse_caption_entities', 'parse_caption_entity', 'parse_entities', 'parse_entity', 'photo', 'pinned_message', 'reply_audio', 'reply_contact', 'reply_document', 'reply_html', 'reply_location', 'reply_markdown', 'reply_media_group', 'reply_photo', 'reply_sticker', 'reply_text', 'reply_to_message', 'reply_venue', 'reply_video', 'reply_video_note', 'reply_voice', 'sticker', 'successful_payment', 'supergroup_chat_created', 'text', 'text_html', 'text_html_urled', 'text_markdown', 'text_markdown_urled', 'to_dict', 'to_json', 'venue', 'video', 'video_note', 'voice']

编辑:如果每个人想知道update对象是否真的包含message.text属性-请看看here来说服自己。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51330846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档