第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重載打印python

重載打印python

藍山帝景 2019-11-19 14:56:37
我可以重載該print函數并從內部調用普通函數嗎?我想做的是在我要print呼叫的特定行之后print,它將呼叫普通行print并將副本寫入文件。我也不知道如何超載print。我不知道如何做變長參數。我會盡快查找,但是 重載print python告訴我我不能print在2.x中重載,這是我正在使用的。
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

對于那些回顧了先前的答案的人,從版本“ Python 2.6”開始,對于原始張貼者的問題有了新的答案。


在Python 2.6及更高版本中,您可以禁用print語句,而使用print函數,然后使用自己的print函數覆蓋print函數:


from __future__ import print_function

# This must be the first statement before other statements.

# You may only put a quoted or triple quoted string, 

# Python comments, other future statements, or blank lines before the __future__ line.


try:

    import __builtin__

except ImportError:

    # Python 3

    import builtins as __builtin__


def print(*args, **kwargs):

    """My custom print() function."""

    # Adding new arguments to the print function signature 

    # is probably a bad idea.

    # Instead consider testing if custom argument keywords

    # are present in kwargs

    __builtin__.print('My overridden print() function!')

    return __builtin__.print(*args, **kwargs)

當然,您現在需要考慮此打印功能僅在模塊范圍內。您可以選擇覆蓋__builtin__.print,但是您需要保存原始文件__builtin__.print; 可能與__builtin__名稱空間混為一談。


查看完整回答
反對 回復 2019-11-19
  • 3 回答
  • 0 關注
  • 1052 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號