Re: Introspection

2010-01-10 Thread m...@infoserv.dk
Thanks Miki and Jason. I knew it could be done :-) -- http://mail.python.org/mailman/listinfo/python-list

Re: Introspection

2010-01-06 Thread Jason Scheirer
On Jan 6, 8:38 am, Steven D'Aprano wrote: > On Wed, 06 Jan 2010 06:53:40 -0800, m...@infoserv.dk wrote: > > I'm looking for a way to make a list of string literals in a class. > > > Example: > > > class A: > >    def method(self): > >        print 'A','BC' > > ExtractLiterals(A) > > ['A','BC'

Re: Introspection

2010-01-06 Thread Steven D'Aprano
On Wed, 06 Jan 2010 06:53:40 -0800, m...@infoserv.dk wrote: > I'm looking for a way to make a list of string literals in a class. > > Example: > > class A: >def method(self): >print 'A','BC' > ExtractLiterals(A) > ['A','BC'] > > Is this possible? Can anyone point me in the rig

Re: Introspection

2010-01-06 Thread Miki
Hello Martin, > I'm looking for a way to make a list of string literals in a class. from inspect import getsourcelines from tokenize import generate_tokens, STRING, NUMBER def is_literal(t): return t[0] in (STRING, NUMBER) def get_lieterals(obj): lines, _ = getsourcelines(obj) readli

Re: introspection question: get return type

2009-05-15 Thread Aahz
In article <4a0d2e07$0$9422$426a7...@news.free.fr>, Bruno Desthuilliers wrote: >Aahz a écrit : >> In article <4a0c6e42$0$12031$426a7...@news.free.fr>, >> Bruno Desthuilliers wrote: >>> Marco Mariani a écrit : Bruno Desthuilliers wrote: > Oh, you meant the "return type" ? Nope, no way.

Re: introspection question: get return type

2009-05-15 Thread Bruno Desthuilliers
Aahz a écrit : In article <4a0c6e42$0$12031$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Marco Mariani a écrit : Bruno Desthuilliers wrote: Oh, you meant the "return type" ? Nope, no way. It just doesn't make sense given Python's dynamic typing. Unless he's really trying to write in N

Re: introspection question: get return type

2009-05-14 Thread George Sakkis
On May 14, 3:55 pm, a...@pythoncraft.com (Aahz) wrote: > In article <4a0c6e42$0$12031$426a7...@news.free.fr>, > Bruno Desthuilliers   wrote: > > >Marco Mariani a écrit : > >> Bruno Desthuilliers wrote: > > >>> Oh, you meant the "return type" ? Nope, no way. It just doesn't make > >>> sense given Py

Re: introspection question: get return type

2009-05-14 Thread Terry Reedy
Aahz wrote: In article <4a0c6e42$0$12031$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Marco Mariani a �crit : Bruno Desthuilliers wrote: Oh, you meant the "return type" ? Nope, no way. It just doesn't make sense given Python's dynamic typing. Unless he's really trying to write in Noht

Re: introspection question: get return type

2009-05-14 Thread Aahz
In article <4a0c6e42$0$12031$426a7...@news.free.fr>, Bruno Desthuilliers wrote: >Marco Mariani a écrit : >> Bruno Desthuilliers wrote: >>> >>> Oh, you meant the "return type" ? Nope, no way. It just doesn't make >>> sense given Python's dynamic typing. >> >> Unless he's really trying to write i

Re: introspection question: get return type

2009-05-14 Thread Bruno Desthuilliers
Marco Mariani a écrit : Bruno Desthuilliers wrote: 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, You mean

Re: introspection question: get return type

2009-05-14 Thread Marco Mariani
Bruno Desthuilliers wrote: 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 mo

Re: introspection question: get return type

2009-05-14 Thread 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

Re: introspection question: get return type

2009-05-14 Thread Bruno Desthuilliers
flam...@gmail.com 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 tim

Re: introspection question: get return type

2009-05-14 Thread Diez B. Roggisch
Diez B. Roggisch wrote: > flam...@gmail.com wrote: > >> 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 fin

Re: introspection question: get return type

2009-05-14 Thread bearophileHUGS
flam...@gmail.com: > 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: >>> foo = lambda x: "x" if x else 1 >>> foo(1) 'x' >>> foo(0) 1 The return type of foo() chang

Re: introspection question: get return type

2009-05-14 Thread Diez B. Roggisch
flam...@gmail.com wrote: > 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-

Re: introspection question

2008-01-07 Thread Peter Otten
Alex K wrote: Please don't top-post. > On 07/01/2008, Peter Otten <[EMAIL PROTECTED]> wrote: >> Alex K wrote: >> >> > What would be the simplest way of enumerating all methods and members >> > (including inherited) of a given object? Thank you. >> >> inspect.getmembers() > Nice thank you. But an

Re: introspection question

2008-01-07 Thread Guilherme Polo
2008/1/7, Alex K <[EMAIL PROTECTED]>: > Nice thank you. But anyway to make it look pretty? > pprint.pprint(inspect.getmembers(someobject)) > On 07/01/2008, Peter Otten <[EMAIL PROTECTED]> wrote: > > Alex K wrote: > > > > > What would be the simplest way of enumerating all methods and members > >

Re: introspection question

2008-01-07 Thread Alex K
Nice thank you. But anyway to make it look pretty? On 07/01/2008, Peter Otten <[EMAIL PROTECTED]> wrote: > Alex K wrote: > > > What would be the simplest way of enumerating all methods and members > > (including inherited) of a given object? Thank you. > > inspect.getmembers() > > Peter > -- > htt

Re: introspection question

2008-01-07 Thread Peter Otten
Alex K wrote: > What would be the simplest way of enumerating all methods and members > (including inherited) of a given object? Thank you. inspect.getmembers() Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: introspection question

2008-01-07 Thread Guilherme Polo
2008/1/7, Alex K <[EMAIL PROTECTED]>: > Hi Guys, > > What would be the simplest way of enumerating all methods and members > (including inherited) of a given object? Thank you. > > Alex > -- > http://mail.python.org/mailman/listinfo/python-list > import inspect inspect.getmembers(yourobject) --

Re: introspection and functions

2007-08-23 Thread Wildemar Wildenburger
Ricardo Aráoz wrote: > Ayaz Ahmed Khan wrote: > >> "James Stroud" typed: >> >>> py> def doit(a, b, c, x=14): >>> ... pass >>> ... >>> py> doit.func_code.co_argcount >>> 4 >>> py> doit.func_code.co_varnames >>> ('a', 'b', 'c', 'x') >>> py> doit.func_defaults >>> (14,) >>> >> Neat. >

Re: introspection and functions

2007-08-23 Thread Ricardo Aráoz
Ayaz Ahmed Khan wrote: > "James Stroud" typed: >> py> def doit(a, b, c, x=14): >> ... pass >> ... >> py> doit.func_code.co_argcount >> 4 >> py> doit.func_code.co_varnames >> ('a', 'b', 'c', 'x') >> py> doit.func_defaults >> (14,) > > Neat. > How do you know the 14 corresponds to x ? -- http

Re: introspection and functions

2007-08-22 Thread Ayaz Ahmed Khan
"James Stroud" typed: > py> def doit(a, b, c, x=14): > ... pass > ... > py> doit.func_code.co_argcount > 4 > py> doit.func_code.co_varnames > ('a', 'b', 'c', 'x') > py> doit.func_defaults > (14,) Neat. -- Ayaz Ahmed Khan I have not yet begun to byte! -- http://mail.python.org/mailman/listinf

Re: introspection and functions

2007-08-22 Thread Scott David Daniels
yagyala wrote: > Hi. I would like to be able to tell, at run time, how many parameters > a function requires. Ideally I would like to be able to tell which are > optional as well. I've tried looking at the functions attributes, but > haven't found one that helps in this. How can I do this? > > Tha

Re: introspection and functions

2007-08-22 Thread Bruno Desthuilliers
yagyala a écrit : > Hi. I would like to be able to tell, at run time, how many parameters > a function requires. Ideally I would like to be able to tell which are > optional as well. I've tried looking at the functions attributes, but > haven't found one that helps in this. How can I do this? >>>

Re: introspection and functions

2007-08-22 Thread James Stroud
yagyala wrote: > Hi. I would like to be able to tell, at run time, how many parameters > a function requires. Ideally I would like to be able to tell which are > optional as well. I've tried looking at the functions attributes, but > haven't found one that helps in this. How can I do this? > > Tha

Re: introspection and functions

2007-08-22 Thread Wildemar Wildenburger
yagyala wrote: > Hi. I would like to be able to tell, at run time, how many parameters > a function requires. Ideally I would like to be able to tell which are > optional as well. I've tried looking at the functions attributes, but > haven't found one that helps in this. How can I do this? > I'v

Re: introspection

2006-09-01 Thread rick
Fredrik Lundh wrote: > brad tilley wrote: > >> How do I import a module and then ask it to show me its methods or >> other aspects about itself during execution? I'd like to do something >> such as this: >> >> import win32api > > dir(win32api) > help(win32api) > > and also > > import inspect

Re: introspection

2006-08-31 Thread Fredrik Lundh
brad tilley wrote: > How do I import a module and then ask it to show me its methods or other > aspects about itself during execution? I'd like to do something such as > this: > > import win32api dir(win32api) help(win32api) and also import inspect help(inspect) -- http://mail.python.org

Re: introspection

2006-08-31 Thread Ben Finney
brad tilley <[EMAIL PROTECTED]> writes: > How do I import a module and then ask it to show me its methods or other > aspects about itself during execution? I'd like to do something such as > this: > > import win32api > > print win32api.methods() > > I'd like to write some test scripts that load

Re: introspection

2006-08-31 Thread John Machin
brad tilley wrote: > How do I import a module and then ask it to show me its methods or other > aspects about itself during execution? I'd like to do something such as > this: > > import win32api > > print win32api.methods() > > I'd like to write some test scripts that load modules and probe them

Re: Introspection Class/Instance Name

2006-04-26 Thread robert
*binarystar* wrote: > Hello there, > > what method would you use to return the name of the class and/or > instance introspectively eg. > > class Bollocks: > > def __init__( self ): > > print self.__method_that_returns_class_name__() > print self.__method_that_ret

Re: Introspection Class/Instance Name

2006-04-26 Thread Duncan Booth
*binarystar* wrote: > class Bollocks: > > def __init__( self ): > > print self.__method_that_returns_class_name__() > print self.__method_that_returns_instance_name__() > > > instance_of_bollocks = Bollocks() > > # Which outputs > > 'Bollocks' > 'i

Re: Introspection Class/Instance Name

2006-04-26 Thread [EMAIL PROTECTED]
What about: py> class A: py. def __init__(self): py. print self.__class__.__name__ py. print str(self) py. def __str__(self): py. return 'instance of %s' % self.__class__.__name__ py. py> a = A() A instance of A py> -- http://mail.python.org/mailman/l

Re: Introspection Class/Instance Name

2006-04-25 Thread Roland Heiber
*binarystar* wrote: > Hello there, > > what method would you use to return the name of the class and/or > instance introspectively eg. > > class Bollocks: > > def __init__( self ): > > print self.__method_that_returns_class_name__() > print self.__method_that_ret

Re: introspection inquiry

2005-02-20 Thread Michael Hoffman
Robin Becker wrote: import inspect class A: _class_name=inspect.currentframe().f_code.co_name def __init__(self,text,_defining_class_name=_class_name): print 'text=',text,'_defining_class_name=',_defining_class_name class B(A): pass b=B('aaa') That won't work, if you, say, wanted to print

Re: introspection inquiry

2005-02-20 Thread Robin Becker
Michael Hoffman wrote: Robin Becker wrote: self.__class__.__name__ Unless I misunderstood the question, that won't work. That will give you the name of the class the object is an instance is of. I think he wants the name of the class the method was defined in. Here's a way to do that using metacla

Re: introspection inquiry

2005-02-20 Thread Michael Hoffman
John Roth wrote: If that's the case, then the inspect module should give the tools to do it without a great deal of angst. Unfortunately, it doesn't give you the class that defined the method, just the class that invoked it. Are you saying that the inspect module *should* give you the tools to do i

Re: introspection inquiry

2005-02-20 Thread Michael Hoffman
Diez B. Roggisch wrote: >[Michael Hoffman]: Unless I misunderstood the question, that won't work. That will give you the name of the class the object is an instance is of. I think he wants the name of the class the method was defined in. Where is the difference? The method is defined in a class - a

Re: introspection inquiry

2005-02-20 Thread John Roth
"Michael Hoffman" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Robin Becker wrote: self.__class__.__name__ Unless I misunderstood the question, that won't work. That will give you the name of the class the object is an instance is of. I think he wants the name of the class the metho

Re: introspection inquiry

2005-02-20 Thread Diez B. Roggisch
> Unless I misunderstood the question, that won't work. That will > give you the name of the class the object is an instance is of. > I think he wants the name of the class the method was defined in. Where is the difference? The method is defined in a class - and an instance is created from that c

Re: introspection inquiry

2005-02-20 Thread Michael Hoffman
Robin Becker wrote: self.__class__.__name__ Unless I misunderstood the question, that won't work. That will give you the name of the class the object is an instance is of. I think he wants the name of the class the method was defined in. Here's a way to do that using metaclasses and Python's magic

Re: introspection inquiry

2005-02-20 Thread Robin Becker
[EMAIL PROTECTED] wrote: ... My question is this: what can be substituted for that will make the example above work? self.__class__.__name__ -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list