防止 Python 中函数重复的最佳做法是什么?
What is the best practice to prevent repetition in functions in Python?
我正在使用 python-telegram-bot 开发一个机器人。我正在大量重复它的某些部分。不要重复的最佳做法是什么?
这是我的两个函数的示例。
def logo_design(update, context):
global keyboard
query = update.callback_query
keyboard[0][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark:", use_aliases=True)} Logo Design', callback_data='6')
bot = context.bot
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text="Please choose one of our services\n",
reply_markup=reply_markup
)
def mob_development(update, context):
global keyboard
query = update.callback_query
keyboard[1][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark: Mobile Development", use_aliases=True)} ', callback_data='0')
bot = context.bot
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text="Please choose one of our services\n",
reply_markup=reply_markup
)
return SECOND
如您所见,我在函数中重复了大部分代码行。我想阻止它。
- 您可以添加一个参数,例如
type
,这将 select 使用哪个键盘。然后你的代码将是。
# I have re-factored the formatting the code
def function_name(update, context, type):
global keyboard
query = update.callback_query
if type == 0:
keyboard[0][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark:", use_aliases=True)} Logo Design',
callback_data='6')
else:
keyboard[1][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark: Mobile Development", use_aliases=True)} ', callback_data='0')
bot = context.bot
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text="Please choose one of our services\n",
reply_markup=reply_markup
)
我正在使用 python-telegram-bot 开发一个机器人。我正在大量重复它的某些部分。不要重复的最佳做法是什么?
这是我的两个函数的示例。
def logo_design(update, context):
global keyboard
query = update.callback_query
keyboard[0][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark:", use_aliases=True)} Logo Design', callback_data='6')
bot = context.bot
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text="Please choose one of our services\n",
reply_markup=reply_markup
)
def mob_development(update, context):
global keyboard
query = update.callback_query
keyboard[1][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark: Mobile Development", use_aliases=True)} ', callback_data='0')
bot = context.bot
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text="Please choose one of our services\n",
reply_markup=reply_markup
)
return SECOND
如您所见,我在函数中重复了大部分代码行。我想阻止它。
- 您可以添加一个参数,例如
type
,这将 select 使用哪个键盘。然后你的代码将是。
# I have re-factored the formatting the code
def function_name(update, context, type):
global keyboard
query = update.callback_query
if type == 0:
keyboard[0][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark:", use_aliases=True)} Logo Design',
callback_data='6')
else:
keyboard[1][0] = InlineKeyboardButton(
f'{emojize(":white_check_mark: Mobile Development", use_aliases=True)} ', callback_data='0')
bot = context.bot
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text="Please choose one of our services\n",
reply_markup=reply_markup
)