Groups
Sign in
Groups
comp.lang.python
Conversations
About
Send feedback
Help
info
Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Learn more
Dynamically call methods where method is known by address vs name
19 views
Skip to first unread message
Malcolm Greene
unread,
Jul 22, 2016, 4:04:43 PM
7/22/16
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
I know I can do the following:
>>> s = 'lower'
>>> getattr(s, 'upper')()
'LOWER'
But how could I do the same if I had the method 'address' (better
name???) vs. method name?
>>> upper_method = s.upper
How do I combine this upper_method with string s to execute the method
and return 'LOWER'?
Thank you,
Malcolm
Michael Selik
unread,
Jul 22, 2016, 4:33:02 PM
7/22/16
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
In [1]: s = 'hello'
In [2]: f = s.upper
In [3]: f()
Out[3]: 'HELLO'
In [4]: g = str.upper
In [5]: g(s)
Out[5]: 'HELLO'
Is that what you're looking for?
Malcolm Greene
unread,
Jul 22, 2016, 5:05:14 PM
7/22/16
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hi Michael,
> Out[3]: 'HELLO'
> In [4]: g = str.upper
> In [5]: g(s)
> Out[5]: 'HELLO'
That's perfect! My mistake was trying to use the method returned by
''.upper vs. str.upper.
Thank you,
Malcolm
0 new messages