2012-06-12 70 views
2

簡單的問題,我想文本追加到每print我打電話的前面,例如,如果我的文字設置爲hello,跑這樣的:追加到每個打印消息

這將打印此:

hellohello there 
hello hi again 

是否有這樣做的任何方式,而不使用函數來使用,而不是print

+0

「風格化打印」不知道我理解這部分,請你重新說明嗎? – Levon

+0

我的意思是創建一個叫'print'hello'+ message'的函數,我想編輯實際的'print'命令。 – user1447941

+2

爲什麼你想避免爲此做一個功能?像'def myPrint(text):print('hello'+ text')是真正的方法。 – Junuxx

回答

3

您可以覆蓋打印按DevPlayer's post here on StackOverflow,在這裏略作修改:

from __future__ import print_function 
# Note: If you are using Python 3 leave this line out 
# This must be the first statement before other statements. 
# You may only put a quoted or triple quoted string, 
# Python comments or blank lines before the __future__ line. 
import sys 

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 
    sys.stdout.write('hello') 
    return __builtins__.print(*args, **kwargs) 

print ("hello there") 
print (" hi again") 

[編輯] ...或DSM建議,你可以避開與此SYS電話:

from __future__ import print_function 
# Note: If you are using Python 3 leave this line out 
# This must be the first statement before other statements. 
# You may only put a quoted or triple quoted string, 
# Python comments or blank lines before the __future__ line. 

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 
    __builtins__.print('hello',end='') 
    return __builtins__.print(*args, **kwargs) 

print ("hello there") 
print (" hi again") 
+0

這只是在我打電話給每個'print'之前在新行上打印'hello'。 – user1447941

+0

進行了更正 - 嘗試改爲。 –

+1

與其使用'sys.stdout.write',您可以將'end ='''參數傳遞給打印函數。 – DSM

1

您無法更改Python 2的打印語句,但可以編寫自己的文件類對象並使用它:

class PrefixedFile(object): 
    def __init__(self, f, prefix): 
     self.f = f 
     self.prefix = prefix 

    def write(self, s): 
     s = s.replace("\n", "\n"+self.prefix) 
     self.f.write(s) 

sys.stdout = PrefixedFile(sys.stdout, "hello: ") 

print "One" 
print "Two" 

請注意,此代碼不起作用,因爲它在第一行中缺少前綴,並在最後添加了一個前綴,但您明白了! :)

+0

你可以從2.6開始...... –

+1

@JonCage:你可以改變打印*功能*,但改變打印*語句*並不容易。 – EOL

+1

是的,好吧,你讓我在那裏:-) –

0

儘管喬恩·凱奇的回答是更換print()功能的一個很好的方式,我會建議,而不是使用自己的打印功能(使用喬恩的代碼):

from __future__ import print_function 
# Note: If you are using Python 3 leave this line out 
# This must be the first statement before other statements. 
# You may only put a quoted or triple quoted string, 
# Python comments or blank lines before the __future__ line. 

def my_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 
    print('hello', end='') 
    print(*args, **kwargs) 

與喬恩的答案唯一的區別是你不覆蓋內置的print()(「猴子修補」)。我提倡這一點,而不是修改print(),因爲這會讓您的代碼更易於維護,因爲每個人都期望print()是內置的。

使用print()功能代替print聲明,my_print(),給人greater flexibility

+0

我同意使用不同名稱的函數更安全,但這不是OP要求的;-) –

+0

@JonCage:是的,他要求替換print *語句*,並且沒有答案提供這個,因爲這很難。 :) – EOL

+1

是的,公平的一點.. –