Can you use "first class" concept on Python methods/attributes?

时间:2016-07-11 22:40:08

标签: python

Can the "first-class" concept be applied to Python methods/attributes like it can functions?

>>>a=sum
>>>a([1,2,3])
6

I would like to do something like:

>>>case='lower'
>>>'UPPERCASE'.case()

To produce the string object 'uppercase'. Otherwise, would I just have to use eval?

1 个答案:

答案 0 :(得分:0)

You can do it this way:

case = str.lower # Take the lower() method of the str class.
case('UPPERCASE') # pass your string as the first argument, which is self.

In this case, Python being explicit about self being the first argument to methods makes this a lot clearer to understand.