keyword

Python: SyntaxError: keyword can't be an expression

Python: SyntaxError: keyword can't be an expression Question: In a Python script I call a function from rpy2, but I get this error: #using an R module res = DirichletReg.ddirichlet(np.asarray(my_values),alphas, log=False, sum.up=False) SyntaxError: keyword can’t be an expression What exactly went wrong here? Asked By: Ricky Robinson || Source Answers: sum.up is not a valid …

Total answers: 5

class Classname(object), what sort of word is 'object' in Python?

class Classname(object), what sort of word is 'object' in Python? Question: When I create a module with its sole content: class Classname(randomobject): pass And I try to run the .py file of the module the interpreter says that randomobject is not defined. But when I do: class Classname(object): pass The module runs just fine. So …

Total answers: 4

Is `id` a keyword in python?

Is `id` a keyword in python? Question: My editor (TextMate) shows id in another colour (when used as variable name) than my usual variable names. Is it a keyword? I don’t want to shade any keyword… Asked By: Aufwind || Source Answers: id is not a keyword in Python, but it is the name of …

Total answers: 4

Python, why elif keyword?

Python, why elif keyword? Question: I just started Python programming, and I’m wondering about the elif keyword. Other programming languages I’ve used before use else if. Does anyone have an idea why the Python developers added the additional elif keyword? Why not: if a: print(“a”) else if b: print(“b”) else: print(“c”) Asked By: raisyn || …

Total answers: 8

Counting the number of distinct keys in a dictionary in Python

Counting the number of distinct keys in a dictionary in Python Question: I have a a dictionary mapping keywords to the repetition of the keyword, but I only want a list of distinct words so I wanted to count the number of keywords. Is there a way to count the number of keywords or is …

Total answers: 6

Normal arguments vs. keyword arguments

Normal arguments vs. keyword arguments Question: How are “keyword arguments” different from regular arguments? Can’t all arguments be passed as name=value instead of using positional syntax? Asked By: mk12 || Source Answers: There are two ways to assign argument values to function parameters, both are used. By Position. Positional arguments do not have keywords and …

Total answers: 10

Getting the keyword arguments actually passed to a Python method

Getting the keyword arguments actually passed to a Python method Question: I’m dreaming of a Python method with explicit keyword args: def func(a=None, b=None, c=None): for arg, val in magic_arg_dict.items(): # Where do I get the magic? print ‘%s: %s’ % (arg, val) I want to get a dictionary of only those arguments the caller …

Total answers: 8

`final` keyword equivalent for variables in Python?

In Python, how can I make unassignable attributes (like ones marked with `final` in Java)? Question: Is there anything in Python that works like the final keyword in Java – i.e., to disallow assigning to a specific attribute of the instances of a class, after those instances have been created? I couldn’t find anything like …

Total answers: 12

Passing a dictionary to a function as keyword parameters

Passing a dictionary to a function as keyword parameters Question: I’d like to call a function in python using a dictionary with matching key-value pairs for the parameters. Here is some code: d = dict(param=’test’) def f(param): print(param) f(d) This prints {‘param’: ‘test’} but I’d like it to just print test. I’d like it to …

Total answers: 4