问题
What does <function at 'somewhere'>
mean? Example:
>>> def main():
... pass
...
>>> main
<function main at 0x7f95cf42f320>
And maybe there is a way to somehow access it using 0x7f95cf42f320
?
回答1:
You are looking at the default representation of a function object. It provides you with a name and a unique id, which in CPython happens to be a memory address.
You cannot access it using the address; the memory address is only used to help you distinguish between function objects.
In other words, if you have two function objects which were originally named main
, you can still see that they are different:
>>> def main(): pass
...
>>> foo = main
>>> def main(): pass
...
>>> foo is main
False
>>> foo
<function main at 0x1004ca500>
>>> main
<function main at 0x1005778c0>
回答2:
It's function's identity, in CPython implementation it's the address of the object in memory.
回答3:
Every object obj has a method obj.__repr__( )
When called, this method returns a string object that is the "official" printable representation of the object obj
When the Python interpreter encounters
a line print obj
in a script
or >>> obj
in a command line,
the method __repr__( ) of the object is called and the value of the representative string returned is displayed on the screen.
The __repr__( ) method of an object can specifically be called by using the built-in function repr( ) with the object's name as argument, to assign the string returned by __repr__( ) to an identifier, so being able to perform operations on this representation.
Only in Python 1 and Python 2, reversed quotes around the name of an object have the same effect than calling repr( ) on it.
Compare:
def main():
pass
if '__repr__' in dir(main):
print ('__repr__ is a method of main\n')
else:
print ('main has no method __repr__\n')
print ('main : %s\n'
'type(main) == %s\n'
% (main,type(main)) )
print ('repr(main) : %s\n'
'type(repr(main)) == %s'
%(repr(main),type(repr(main))) )
# Only in Python 1 and Python 2, string conversions with
# reversed quotes produce the same result as repr():
print ('\n`main` : %s\n'
'type(`main`) == %s'
% (`main`,type(`main`)) )
result
__repr__ is a method of main
main : <function main at 0x00FB2930>
type(main) == <type 'function'>
repr(main) : <function main at 0x00FB2930>
type(repr(main)) == <type 'str'>
.
In <function main at 0x00FB2930>
, the part 0x00FB2930
represents the memory address of the object (here a function), that is to say an integer that references the location of the object in the RAM.
0x00FB2930
is an hexinteger, that is to say a literal representing the value of the memory address in base 16.
This memory address is precisely returned by the built-in function id()
, whose value is printed as a decimalinteger literal, that is to say its representation in base 10.
print ('repr(main) : %s\n'
'type(repr(main)) == %s\n'
% (repr(main),
type(repr(main))) )
hex_address = repr(main)[18:-1]
print ('hex_address = repr(main)[18:-1] defined\n'
'hex_address : %s\n'
'type(hex_address) == %s\n'
'int(hex_address , 16) : %s\n'
'type(int(hex_address , 16)) : %s\n'
% (hex_address,
type(hex_address),
int(hex_address , 16),
type(int(hex_address , 16))) )
print ('id(main) : %s\n'
'type(id(main)) == %s\n'
'hex(id(main) : %s\n'
'type(hex(id(main)) : %s'
% (id(main),
type(id(main)),
hex(id(main)),
type(hex(id(main)))) )
result
repr(main) : <function main at 0x00FB2930>
type(repr(main)) == <type 'str'>
hex_address = repr(main)[18:-1] defined
hex_address : 0x00FB2930
type(hex_address) == <type 'str'>
int(hex_address , 16) : 16460080
type(int(hex_address , 16)) : <type 'int'>
id(main) : 16460080
type(id(main)) == <type 'int'>
hex(id(main) : 0xfb2930
type(hex(id(main)) : <type 'str'>
回答4:
In CPython is simply the address of an object in memory. All objects have this, not only functions.
来源:https://stackoverflow.com/questions/19333598/in-python-what-does-function-at-mean