Express a method like a keyword [duplicate] - python

This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Closed 4 years ago.
How can I express a method (e.g. dir(os)) like a keyword (e.g. dir_ os)? Is it even possible?
The opposite is quite easy to achieve:
# `assert` expressed like a method
def assert_(x): assert x
assert_(1 == 1)

No, it's not possible. Parentheses are required in order to call a function or method.

Related

Pass a str variable for flag in Python function? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 1 year ago.
In python want to do something like this:
def func(*args, flag):
do something
if cond A:
flag="some A"
else:
flag="some B"
Then I found the flag passed is always unchanged.
If I change the flag to be a list, then yes it works but is ugly.
What is the proper way to achieve the above?

Is defining a constant with a loop inside a class compliant with PEP8? [duplicate]

This question already has answers here:
Should class-specific "constants" still be declared at module level?
(2 answers)
Closed 2 years ago.
I would like to use a mathematical serie to define a constant inside a class. I would like to know whether this is compliant with the PEP8. Thank you in advance. Please see image below
That looks fine but SIZE = [2 ** i for i in range(5)] is probably cleaner.

I have error when I use exec() to call functions in python 3 [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Use a string to call function in Python [duplicate]
(3 answers)
Closed 4 years ago.
I have this simple function:
def fu():
return "great"
I need to call it by using a string,
So I tried this:
print(exec("fu()"))
But the the output I got was:
None
How do i fix it?
As in comments says you can not use exec for this purpose.
but eval will do what you want, full doc here:
>> eval('fu()')
"great"
Note that using eval is not the best practice.
There is a better way to access this function with globals or locals based on where you define your function, and I think it's better to use this instead of eval:
>> globals()['fu']()
"great"

Variable arguments in python -- Possible or not? [duplicate]

This question already has answers here:
Can a variable number of arguments be passed to a function?
(6 answers)
Closed 8 years ago.
How do you write a function in Python with variable arguments into the function and return variable number of outputs? Is this even possible with the constraints?
You mean like this. This is quite similar to ellipses in Java. When supplied with a variable amount of arguments, you can unpack these arguments as a list which you can manipulate as you deem necessary.
def func(*args):
print len(args) # num of vars
yes you can, take a look at splat operator
def my_function(*a): #here a will be list or tuple depend wt u passed
# do your stuff with a
**a for dictionary

Join two strings into a callable string 'moduleA' + 'func1' into moduleA.func1() [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 9 years ago.
I got this:
tu = ("func1", "func2", "func3")
And with the operation I am looking for I would get this for the first string:
moduleA.func1()
I know how to concatenate strings, but is there a way to join into a callable string?
getattr(moduleA, 'func1')() == moduleA.func1()
You should use getattr builtin function. Try:
getattr(moduleA, 'func1')()
If you mean get a function or method on a class or module, all entities (including classes, modules, functions, and methods) are objects, so you can do a func = getattr(thing 'func1') to get the function, then func() to call it.

Categories