简体   繁体   English

Python 3.3 Reversed()函数和Shell消息含义

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

I have this following block of code and I am getting this message in the shell... 我有以下代码块,我在shell中收到此消息...

"function word_switch at 0x102b67a70" “函数word_switch在0x102b67a70”

What does this mean and why does Python print this instead of returning the function? 这是什么意思,为什么Python打印这个而不是返回函数?

Thanks 谢谢

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

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

print(word_switch(D))

You need to generate the reversed list and return it, not the function itself. 您需要生成反转列表并返回它,而不是函数本身。 You can do this 你可以这样做

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))

Output 产量

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

What does it mean? 这是什么意思? The output means that the function word_switch lives in that memory position. 输出意味着函数word_switch位于该存储器位置。

Why are you getting it? 你为什么得到它? Because you asked for it. 因为你要求它。 Beware that in the way your function is defined, when you call it you get the function object itself and then you print it. 请注意,在定义函数的方式中,当您调用它时,您将获得函数对象本身,然后打印它。

It happens the very same thing every time you print a function object, for instance: 每次打印函数对象时都会发生相同的事情,例如:

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

If what you want is just reverse a list you can simply use the reverse method of lists objects as suggested in the comments to the OP. 如果您想要的只是反转列表,您可以简单地使用列表对象的reverse方法,如OP的注释中所建议的那样。 It is not necessary to create you own function. 没有必要创建自己的功能。 If , for some reason, you need your own function then you can write it in the way suggested by @thefourtheye in his answer. 如果出于某种原因,你需要自己的功能,那么你可以按照@thefourtheye在他的回答中建议的方式来编写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM