簡體   English   中英

Python 3.3 Reversed()函數和Shell消息含義

[英]Python 3.3 Reversed() function and Shell message meaning

我有以下代碼塊,我在shell中收到此消息...

“函數word_switch在0x102b67a70”

這是什么意思,為什么Python打印這個而不是返回函數?

謝謝

D= ['foo', 'bar', 'OMG']

def word_switch (D: list) -> list:
    for i in D:
        reversed(i)
    return word_switch

print(word_switch(D))

您需要生成反轉列表並返回它,而不是函數本身。 你可以這樣做

D = ['foo', 'bar', 'OMG']

def word_switch (D: list) -> list:
    return ["".join(reversed(i)) for i in D]
    #return [i[::-1] for i in D]  # Using slicing

print(word_switch(D))

產量

['oof', 'rab', 'GMO']

這是什么意思? 輸出意味着函數word_switch位於該存儲器位置。

你為什么得到它? 因為你要求它。 請注意,在定義函數的方式中,當您調用它時,您將獲得函數對象本身,然后打印它。

每次打印函數對象時都會發生相同的事情,例如:

>>> def f(x):
...       x
>>> print f
<function f at 0xb72eda3c>
>>>

如果您想要的只是反轉列表,您可以簡單地使用列表對象的reverse方法,如OP的注釋中所建議的那樣。 沒有必要創建自己的功能。 如果出於某種原因,你需要自己的功能,那么你可以按照@thefourtheye在他的回答中建議的方式來編寫它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM