On 22 nov, 02:38, Chris <[EMAIL PROTECTED]> wrote:
> Hello everyone,
> I have a quick python question. Is they a better way of calling a
> method dynamically then what I have below? Is there a more elegant
> approach? Thanks in advance.
> try:
> push = getattr(self, 'get_%s_content' % self.n
On 22 nov, 13:07, "Steve McConville" <[EMAIL PROTECTED]>
wrote:
> > On Sat, Nov 22, 2008 at 2:38 AM, Chris <[EMAIL PROTECTED]> wrote:
> >> Hello everyone,
> >> I have a quick python question. Is they a better way of calling a
> >> method dynamically then what I have below? Is there a more elegant
On 22 nov, 14:05, Ivan Sagalaev <[EMAIL PROTECTED]> wrote:
> Chris wrote:
> > try:
> > push = getattr(self, 'get_%s_content' % self.name)
> > except AttributeError:
> > raise "Method does not exist"
> > if callable(push):
> > push(**argv)
>
> There are a couple of problems with this co
getattr is definitely preferable to self.__class__.__dict__
--Ned.
http://nedbatchelder.com
Henry Andrews wrote:
> I've aways been under the impression that direct use of __dict__ was
> discouraged. Something along the lines of __dict__ being an
> implementation detail while getattr and hasattr
I've aways been under the impression that direct use of __dict__ was
discouraged. Something along the lines of __dict__ being an
implementation detail while getattr and hasattr are the proper
interfaces. But I might be wrong.
thanks,
-henry
On Nov 22, 4:07 am, "Steve McConville" <[EMAIL PROTEC
> On Sat, Nov 22, 2008 at 2:38 AM, Chris <[EMAIL PROTECTED]> wrote:
>> Hello everyone,
>> I have a quick python question. Is they a better way of calling a
>> method dynamically then what I have below? Is there a more elegant
>> approach? Thanks in advance.
>>
try:
self.__class__.__dict__[met
Ivan Sagalaev wrote:
> if hasattr(self, 'get_%s_content' % self.name):
> raise Exception('Method does not exists')
Typo: there should be `if not hasattr`, obviously.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Grou
Chris wrote:
> try:
> push = getattr(self, 'get_%s_content' % self.name)
> except AttributeError:
> raise "Method does not exist"
> if callable(push):
> push(**argv)
There are a couple of problems with this code.
1. It's more readable to call hasattr than to catch an exception from
AFAIK no (except for perhaps some different exception handling but
getattr is IMO the way to go), but what don't you like about this
approach?
-- Horst
On Sat, Nov 22, 2008 at 2:38 AM, Chris <[EMAIL PROTECTED]> wrote:
> Hello everyone,
> I have a quick python question. Is they a better way of ca
Hello everyone,
I have a quick python question. Is they a better way of calling a
method dynamically then what I have below? Is there a more elegant
approach? Thanks in advance.
try:
push = getattr(self, 'get_%s_content' % self.name)
except AttributeError:
raise "Method does not exist"
if
10 matches
Mail list logo