Re: [Tutor] getattr works sometimes

2012-10-05 Thread Tino Dai
On Tue, Oct 2, 2012 at 8:39 PM, Steven D'Aprano wrote: > On 03/10/12 04:20, Tino Dai wrote: > >> and the get_class class works sometime for finding modules within a certain directory. If the get_class doesn't work, it throws an AttributeError. >>> >>> I don't really understand what

Re: [Tutor] getattr works sometimes

2012-10-02 Thread Steven D'Aprano
On 03/10/12 04:20, Tino Dai wrote: and the get_class class works sometime for finding modules within a certain directory. If the get_class doesn't work, it throws an AttributeError. I don't really understand what you mean by this. Can you copy and paste the actual error message (all of it)?

Re: [Tutor] getattr works sometimes

2012-10-02 Thread Steven D'Aprano
On 03/10/12 03:44, Tino Dai wrote: Hi All, I'm using the get_class from: http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname Do you mean this function? def get_class( kls ): parts = kls.split('.') module = ".".join(parts[:-1]) m

Re: [Tutor] getattr works sometimes

2012-10-02 Thread Oscar Benjamin
On 2 October 2012 19:27, Tino Dai wrote: > On Tue, Oct 2, 2012 at 2:20 PM, Tino Dai wrote: and the get_class class works sometime for finding modules within a certain directory. If the get_class doesn't work, it throws an AttributeError. >>> >>> I don't really understand what you m

Re: [Tutor] getattr works sometimes

2012-10-02 Thread Tino Dai
On Tue, Oct 2, 2012 at 2:20 PM, Tino Dai wrote: >>> and the get_class class works sometime for finding modules within a >>> certain directory. If the get_class >>> doesn't work, it throws an AttributeError. >> >> I don't really understand what you mean by this. Can you copy and >> paste the actual

Re: [Tutor] getattr works sometimes

2012-10-02 Thread Tino Dai
>> and the get_class class works sometime for finding modules within a >> certain directory. If the get_class >> doesn't work, it throws an AttributeError. > > I don't really understand what you mean by this. Can you copy and > paste the actual error message (all of it)? > >> >> The module exists i

Re: [Tutor] getattr works sometimes

2012-10-02 Thread Oscar Benjamin
On 2 October 2012 18:44, Tino Dai wrote: > Hi All, Hi Tino > > I'm using the get_class from: > > http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname Can you show the relevant portion of your code please? > > and the get_class class works sometim

[Tutor] getattr works sometimes

2012-10-02 Thread Tino Dai
Hi All, I'm using the get_class from: http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname and the get_class class works sometime for finding modules within a certain directory. If the get_class doesn't work, it throws an AttributeError. The modul

Re: [Tutor] getattr help

2012-03-10 Thread Peter Otten
mjole...@gmail.com wrote: > What is the purpose of getattr? Why not just use help or am I completely > misunderstanding this? > >>From what I read, getattr allows you to get a reference to a function >>without knowing its name until runtime. > > However, the example provided is: > > li = ['larr

[Tutor] getattr help

2012-03-09 Thread mjolewis
What is the purpose of getattr? Why not just use help or am I completely misunderstanding this? >From what I read, getattr allows you to get a reference to a function without >knowing its name until runtime. However, the example provided is: li = ['larry', 'curly] getattr(li, 'pop') It seems

Re: [Tutor] getattr()

2010-08-05 Thread bob gailer
On 8/4/2010 4:32 PM, Huy Ton That wrote: I have a side question, I am using python 2.7. Why do you use class A: instead of class A(object): ? My example does not depend on old / new style classes. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tuto

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
class A: def call_by_name(self, func, data): f = A.__dict__.get(func, self.output_text) return f(data) def output_text(self, data):return data def output_hex(self, data):return '\\x' + data a = A() data = 'bar' print a.call_by_name('output_text', data) print a.call_by_name('output_he

Re: [Tutor] getattr()

2010-08-04 Thread Huy Ton That
I have a side question, I am using python 2.7. Why do you use class A: instead of class A(object): ? -Huy On Wed, Aug 4, 2010 at 4:08 PM, bob gailer wrote: > On 8/4/2010 4:04 PM, bob gailer wrote: > >> On 8/4/2010 3:44 PM, Huy Ton That wrote: >> >>> Oh, that's right, I should have tried to ex

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
On 8/4/2010 4:04 PM, bob gailer wrote: On 8/4/2010 3:44 PM, Huy Ton That wrote: Oh, that's right, I should have tried to example in the interpreter instead of in my head:P Say Bob, Is that the preferred method over something like: I would prefer to create a class and make these functions cl

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
On 8/4/2010 3:44 PM, Huy Ton That wrote: Oh, that's right, I should have tried to example in the interpreter instead of in my head:P Say Bob, Is that the preferred method over something like: I would prefer to create a class and make these functions class methods. class A: def output_text

Re: [Tutor] getattr()

2010-08-04 Thread Huy Ton That
Oh, that's right, I should have tried to example in the interpreter instead of in my head:P Say Bob, Is that the preferred method over something like: >>> import __main__ as main On Wed, Aug 4, 2010 at 3:32 PM, bob gailer wrote: > On 8/4/2010 1:23 PM, Pete wrote: > > Hi, > > I'm trying to u

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
On 8/4/2010 1:23 PM, Pete wrote: Hi, I'm trying to understand the syntax for reflection in python. I was wondering about this. From the example on diveintopython: http://diveintopython.org/power_of_introspection/index.html import statsout def output(data, format="text"): output_func

Re: [Tutor] getattr()

2010-08-04 Thread Huy Ton That
*You can do like below Pete. Where globals has a reference to all functions in this space.* def output(data, format="text"): output_function = getattr(globals()['FUNCTION'], "output_%s" % format, statsout.output_text) return output_function(data) On Wed, Aug 4, 2010 at 3:18 PM, Pete wr

Re: [Tutor] getattr()

2010-08-04 Thread Pete
Hey Huy, thanks. But what is the first parameter in this case? (Note there is nothing here I'm trying to accomplish except understand). In the original example, 'statsout' is the name of the module. In this case there is no module so why can you substitute 'output_text'? thanks, Pete On 2010

[Tutor] getattr()

2010-08-04 Thread Pete
Hi, I'm trying to understand the syntax for reflection in python. I was wondering about this. From the example on diveintopython: http://diveintopython.org/power_of_introspection/index.html import statsout def output(data, format="text"): output_function = getattr(statsout, "output_%s"

Re: [Tutor] getattr()

2006-07-26 Thread Alan Gauld
Janos, I'm confused... # #This should be the complete file def OnMenuFindMe(): print 'You found me' f = getattr(What_Should_It_Be???, 'OnMenuFindMe') f() # You are trying to get a reference to a function in the same file and whose name you know? So why not

Re: [Tutor] getattr()

2006-07-26 Thread Kent Johnson
Tony Cappellini wrote: > From: Kent Johnson <[EMAIL PROTECTED]> > > >>> or you can import __main__, which is the top-level module > Do you mean __main__ is the top level module for every python > program/script? > Yes, at least in the standard Python runtime, I'm not sure what happens when r

[Tutor] getattr()

2006-07-26 Thread János Juhász
Dear Kent, >>You can look up the function in the globals() dict, or you can import >>__main__, which is the top-level module, and use getattr() on it. Or you >>could wrap your functions in a class... >> >>def OnMenuFindMe(): >>    print 'You found me' >> >>f = globals()['OnMenuFindMe'] >> >>f()

Re: [Tutor] getattr()

2006-07-26 Thread Kent Johnson
János Juhász wrote: > > Dear All, > > I want to use getattr() to collect a list with all the functions on my > simple script those has got some functionname like 'On'. > > #This should be the complete file > def OnMenuFindMe(): > print 'You found me' > > f = getattr(What_Should_It_Be???, '

[Tutor] getattr()

2006-07-26 Thread János Juhász
Dear All, I want to use getattr() to collect a list with all the functions on my simple script those has got some functionname like 'On'. #This should be the complete file def OnMenuFindMe():     print 'You found me' f = getattr(What_Should_It_Be???, 'OnMenuFindMe') f() #Till here It can

Re: [Tutor] getattr of functions

2005-11-24 Thread Kent Johnson
Negroup - wrote: > Hi all! I'm here again with a question about introspection. > > My module stores a set of functions. I need to know, from another > script, if a particular function of this module "is enabled" (it > means, if it shall be executed by the caller script). I looked for > some intros

Re: [Tutor] getattr of functions

2005-11-24 Thread Negroup -
2005/11/24, Liam Clarke <[EMAIL PROTECTED]>: > What do you mean enabled? > > If it's imported into the namespace you can call it... > > Err, can you clarify on the enabling, what context are you using it > in, and what are you trying to achieve? Hi, sorry. I'll try to present the actors omitting t

Re: [Tutor] getattr of functions

2005-11-24 Thread Liam Clarke
What do you mean enabled? If it's imported into the namespace you can call it... Err, can you clarify on the enabling, what context are you using it in, and what are you trying to achieve? On 11/25/05, Negroup - <[EMAIL PROTECTED]> wrote: > Hi all! I'm here again with a question about introspec

[Tutor] getattr of functions

2005-11-24 Thread Negroup -
Hi all! I'm here again with a question about introspection. My module stores a set of functions. I need to know, from another script, if a particular function of this module "is enabled" (it means, if it shall be executed by the caller script). I looked for some introspective builtin/function, but