简体   繁体   中英

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

"function word_switch at 0x102b67a70"

What does this mean and why does Python print this instead of returning the function?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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