Re: Get bound method by name

2009-03-03 Thread Chris Rebert
On Tue, Mar 3, 2009 at 12:54 PM, Steve Holden  wrote:
> Graham Breed wrote:
>> Johannes Bauer wrote:
>>> Hello group,
>>>
>>> I'm looking for a Python function but have forgotten it's name.
>>> Essentially what I want is:
>>>
>>> class Foo():
>>>     def bar(self):
>>>         pass
>>>
>>> x = Foo()
>>> y = x.MAGIC("bar")
>>> print(y)
>>> >
>>>
>>> So the question is: How is the magic function called which returns me
>>> the bound method of a class instance by its name? I know there was a way
>>> but just can't remember...
>>
>> y = getattr(x, "bar")
>
> Or, as a method call (which was requested):

The OP was a bit inconsistent. He only used the word "function", but
his pseudocode used a method.
Either way, one shouldn't normally use magic methods directly.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Get bound method by name

2009-03-03 Thread Steve Holden
Graham Breed wrote:
> Johannes Bauer wrote:
>> Hello group,
>>
>> I'm looking for a Python function but have forgotten it's name.
>> Essentially what I want is:
>>
>> class Foo():
>> def bar(self):
>> pass
>>
>> x = Foo()
>> y = x.MAGIC("bar")
>> print(y)
>> >
>>
>> So the question is: How is the magic function called which returns me
>> the bound method of a class instance by its name? I know there was a way
>> but just can't remember...
> 
> y = getattr(x, "bar")

Or, as a method call (which was requested):

y = x.__getattr__("bar")

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

--
http://mail.python.org/mailman/listinfo/python-list


Re: Get bound method by name

2009-03-03 Thread Graham Breed

Johannes Bauer wrote:

Hello group,

I'm looking for a Python function but have forgotten it's name.
Essentially what I want is:

class Foo():
def bar(self):
pass

x = Foo()
y = x.MAGIC("bar")
print(y)
>

So the question is: How is the magic function called which returns me
the bound method of a class instance by its name? I know there was a way
but just can't remember...


y = getattr(x, "bar")
--
http://mail.python.org/mailman/listinfo/python-list


Re: Get bound method by name

2009-03-03 Thread John Machin
On Mar 3, 10:12 pm, Johannes Bauer  wrote:
> Hello group,
>
> I'm looking for a Python function but have forgotten it's name.
> Essentially what I want is:
>
> class Foo():
>         def bar(self):
>                 pass
>
> x = Foo()
> y = x.MAGIC("bar")
> print(y)
> >
>
> So the question is: How is the magic function called which returns me
> the bound method of a class instance by its name? I know there was a way
> but just can't remember...

getattr(x, "bar")

--
http://mail.python.org/mailman/listinfo/python-list


Re: Get bound method by name

2009-03-03 Thread Chris Rebert
On Tue, Mar 3, 2009 at 3:12 AM, Johannes Bauer  wrote:
> Hello group,
>
> I'm looking for a Python function but have forgotten it's name.
> Essentially what I want is:
>
> class Foo():
>        def bar(self):
>                pass
>
> x = Foo()
> y = x.MAGIC("bar")

getattr() is the function you seek.

y = getattr(x, "bar")

> print(y)
> >

Cheers,
Chris

-- 
Shameless self-promotion:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Get bound method by name

2009-03-03 Thread Johannes Bauer
Hello group,

I'm looking for a Python function but have forgotten it's name.
Essentially what I want is:

class Foo():
def bar(self):
pass

x = Foo()
y = x.MAGIC("bar")
print(y)
>

So the question is: How is the magic function called which returns me
the bound method of a class instance by its name? I know there was a way
but just can't remember...

Thanks in advance,
Kind regards,
Johannes

-- 
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
 -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
 <48d8bf1d$0$7510$54022...@news.sunrise.ch>
--
http://mail.python.org/mailman/listinfo/python-list