introspection question: get return type

F

flamz3d

Hello,
I am wondering if it's possible to get the return value of a method
*without* calling it using introspection?

something like this, suppose I want to know the type of the return
value for the method "getsomething". Is it possible to get it without
actually calling 'getsomething'?

ex:
import mystuff
print dir (mystuff.getsomething)
 
D

Diez B. Roggisch

Hello,
I am wondering if it's possible to get the return value of a method
*without* calling it using introspection?

Nope. All that's possible to see if there is a implicit or explicit return
through dis.disassemble - if you find "LOAD_CONST None" before any
return-statement, you know that you don't anything else.

Diez
 
B

bearophileHUGS

(e-mail address removed):
I am wondering if it's possible to get the return value of a method
*without* calling it using introspection?

Python is dynamically typed, so you can create a function like this:
1

The return type of foo() changes according to the input value.
In general there's not much hope to know the return type if you don't
actually run the function.

Bye,
bearophile
 
D

Diez B. Roggisch

Diez said:
Nope. All that's possible to see if there is a implicit or explicit return
through dis.disassemble - if you find "LOAD_CONST None" before any
return-statement, you know that you don't anything else.

Gosh, this is barely intelligible...

What I wanted to say is that you can check if a function only returns
constant values, which most of the time will be implicit "return None"
statements, as in this:

def foo():
a = 10

dis.disassemble(foo.func_code)
....
2 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
Diez
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Hello,
I am wondering if it's possible to get the return value of a method
*without* calling it

Getting the return *value* without calling the function ? heck, that
would be really helpful - we'd save quiet a lot on function call
overhead and function execution time !-)
using introspection?

something like this, suppose I want to know the type of the return
value for the method "getsomething". Is it possible to get it without
actually calling 'getsomething'?

Oh, you meant the "return type" ? Nope, no way. It just doesn't make
sense given Python's dynamic typing.
 
B

bearophileHUGS

On the other hand, generally good programming practice suggests you to
write functions that have a constant return type. And in most programs
most functions are like this. This is why ShedSkin can indeed infer
the return type of functions in "good behaved" programs. To do this
ShedSkin uses a quite refined (and slow if written in Python) type
inference algorithm.

Bye,
bearophile
 
M

Marco Mariani

Bruno said:
Oh, you meant the "return type" ? Nope, no way. It just doesn't make
sense given Python's dynamic typing.

I thought that the OP was writing a tool to document not-very-dynamic code.

Unless he's really trying to write in Nohtyp, the language where value
types are more important than values ;)
 
G

George Sakkis

Marco's spelling is correct.  Try "Nohtyp".reverse()

I tried it:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'reverse'

This newfangled Nohtyp language is no good, I want my money back.

George
 
B

Bruno Desthuilliers

Aahz a écrit :
Marco's spelling is correct. Try "Nohtyp".reverse()

Aazh... I know I'm a bit dumb sometimes, but do you *really* think I had
failed to spot this ???
 
A

Aahz

Aahz a écrit :

Aazh... I know I'm a bit dumb sometimes, but do you *really* think I had
failed to spot this ???

<shrug> IIRC, English isn't your first language, so I figured better
safe than sorry; humor often mistranslates. My apologies for casting
aspersions on your linguistic skills.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"In 1968 it took the computing power of 2 C-64's to fly a rocket to the moon.
Now, in 1998 it takes the Power of a Pentium 200 to run Microsoft Windows 98.
Something must have gone wrong." --/bin/fortune
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top