1

I want to call built in str functions manually (in this case as randomness) without using eval

funcs = [str.lower, str.upper]
random.choice(self.funcs)("test")

or

"test".(random.choice(self.funcs))
1
  • It looks like you already know how to randomly choose string methods. What more are you looking for? Commented Nov 9, 2019 at 13:51

1 Answer 1

1

You can do this

>>> funcs = [str.lower, str.upper]
>>> random.choice(funcs)('Foo')

Instead of calling a string's method, like 'Foo'.lower(), you call the str class's method, and provide your string as the instance of string that the method is called upon. Like this:

>>> str.lower('FOO')
'foo'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.