function-call

f-string: unmatched '(' in line with function call

f-string: unmatched '(' in line with function call Question: I’m trying to use f-strings in python to substitute some variables into a string that I’m printing, and I’m getting a syntax error. Here’s my code: print(f"{index+1}. {value[-1].replace("[Gmail]/", ”)}") I only started having the problem after I added the replace. I’ve checked plenty of times and …

Total answers: 4

Does the order of functions in a Python script matter?

Does the order of functions in a Python script matter? Question: Let’s say I have two functions in my script: sum_numbers and print_sum. Their implementation is like this: def sum_numbers(a, b): return a + b def print_sum(a, b): print(sum_numbers(a, b)) So my question is: does the order in which the function are written matter? If …

Total answers: 3

Using print() (the function version) in Python2.x

Using print() (the function version) in Python2.x Question: I understand the difference between a statement and an expression, and I understand that Python3 turned print() into a function. However I ran a print() statement surrounded with parenthesis on various Python2.x interpreters and it ran flawlessly, I didn’t even have to import any module. My question: …

Total answers: 3

What do double parentheses mean in a function call? e.g. func(foo)(bar)

What do double parentheses mean in a function call? e.g. func(foo)(bar) Question: I use this idiom all the time to print a bunch of content to standard out in utf-8 in Python 2: sys.stdout = codecs.getwriter(‘utf-8’)(sys.stdout) But to be honest, I have no idea what the (sys.stdout) is doing. It sort of reminds me of …

Total answers: 3

Converting list to *args when calling function

Converting list to *args when calling function Question: In Python, how do I convert a list to *args? I need to know because the function scikits.timeseries.lib.reportlib.Report.__init__(*args) wants several time_series objects passed as *args, whereas I have a list of timeseries objects. Asked By: andreas-h || Source Answers: You can use the * operator before an …

Total answers: 3

How to get (sub)class name from a static method in Python?

How to get (sub)class name from a static method in Python? Question: If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): # code pass Is it possible for a function call Foo.bar() to determine the class name Foo? Asked By: Jean-Pierre Chauvel || Source Answers: Replace the staticmethod with a classmethod. …

Total answers: 3