parameter-passing

How can I pass a positional argument to an optional argument in python functions?

How can I pass a positional argument to an optional argument in python functions? Question: I am writing a gradient descent function for linear regression and want to include a default initial guess of parameters as a numpy zero array whose length is determined by the shape of an input 2D array. I tried writing …

Total answers: 2

Set constant for arg parse when using nargs '*'

Set constant for arg parse when using nargs '*' Question: I have a setup like this. What I want to do is to send a constant value if only the -e/–exp are sent, and if -p/–plot are sent then it should only do the plotting. So a default value will not work, as it then …

Total answers: 1

Count how many arguments passed as positional

Count how many arguments passed as positional Question: If I have a function def foo(x, y): pass how can I tell, from inside the function, whether y was passed positionally or with its keyword? I’d like to have something like def foo(x, y): if passed_positionally(y): print(‘y was passed positionally!’) else: print(‘y was passed with its …

Total answers: 6

How can i Pass Variable from flask) o HTML without render template?

How can i Pass Variable from flask) o HTML without render template? Question: I’m new to using Flask and I’ve just been trying to pass a variable to web pages. I know how pass variable in render_template method but now I am trying a realtime streaming app. It uses different methods like .responses and yields …

Total answers: 1

What do * (single star) and / (slash) do as independent parameters?

What do * (single star) and / (slash) do as independent parameters? Question: In the following function definition, what do the * and / account for? def func(self, param1, param2, /, param3, *, param4, param5): print(param1, param2, param3, param4, param5) NOTE: Not to mistake with the single|double asterisks in *args | **kwargs (solved here) Asked …

Total answers: 2

Pass nested dictionary location as parameter in Python

Pass nested dictionary location as parameter in Python Question: If I have a nested dictionary I can get a key by indexing like so: >>> d = {‘a’:{‘b’:’c’}} >>> d[‘a’][‘b’] ‘c’ Am I able to pass that indexing as a function parameter? def get_nested_value(d, path=[‘a’][‘b’]): return d[path] Obviously, this is incorrect, I get TypeError: list …

Total answers: 4

how do I pass self.selected(i)? – Python

how do I pass self.selected(i)? – Python Question: from tkinter import * class App: def __init__(self, master): frame = Frame(master) frame.grid() self.grid_data_table = [“NONE”] self.D_Text = ” ” self.a = 0 self.b = 0 for i in range(1,100 + 1): self.grid_data_table.extend([i]) self.grid_data_table[i] = Button(frame, text=self.D_Text, font=”bold”, command=lambda: self.selected(i)) self.a = self.a + 1 self.grid_data_table[i].grid(row=self.b, column=self.a) …

Total answers: 2

Using an OrderedDict in **kwargs

Using an OrderedDict in **kwargs Question: Is it possible to pass an OrderedDict instance to a function which uses the **kwargs syntax and retain the ordering? What I’d like to do is : def I_crave_order(**kwargs): for k, v in kwargs.items(): print k, v example = OrderedDict([(‘first’, 1), (‘second’, 2), (‘third’, -1)]) I_crave_order(**example) >> first 1 …

Total answers: 3

Python, SQLAlchemy pass parameters in connection.execute

Python, SQLAlchemy pass parameters in connection.execute Question: I am using SQLAlchemy connection.execute(sql) to transform select results to array of maps. Have following code def __sql_to_data(sql): result = [] connection = engine.connect() try: rows = connection.execute(sql) for row in rows: result_row = {} for col in row.keys(): result_row[str(col)] = str(row[col]) result.append(result_row) finally: connection.close() return result and …

Total answers: 2