Passing extra arguments through connect
The problem can be solved in 2 ways:
Using lambda functions:
In general:
obj.signal.connect(lambda param1, param2, ..., arg1=val1, arg2= value2, ... : fun(param1, param2,... , arg1, arg2, ....))
def fun(param1, param2,... , arg1, arg2, ....):
[...]
where:
- param1, param2, ... : are the parameters sent by the signal
- arg1, arg2, ...: are the extra parameters that you want to spend
In your case:
self.buttonGroup.buttonClicked['int'].connect(lambda i: self.input(i, "text"))
@pyqtSlot(int)
def input(self, button_or_id, DiffP):
if isinstance(button_or_id, int):
if button_or_id == 0:
self.TotalInput[0].setText(DiffP)
elif button_or_id == 1:
self.TotalInput[54].setText('1')
Using functools.partial
:
In general:
obj.signal.connect(partial(fun, args1, arg2, ... ))
def fun(arg1, arg2, ..., param1, param2, ...):
[...]
where:
- param1, param2, ... : are the parameters sent by the signal
- arg1, arg2, ...: are the extra parameters that you want to send
In your case:
from functools import partial
[...]
self.buttonGroup.buttonClicked['int'].connect(partial(self.input, "text"))
@pyqtSlot(int)
def input(self, DiffP, button_or_id):
if isinstance(button_or_id, int):
if button_or_id == 0:
self.TotalInput[0].setText(DiffP)
elif button_or_id == 1:
self.TotalInput[54].setText('1')
QML connect() passing extra arguments
You can create a wrapper function at a place that has access to the currentIndex
function wrapFunctionB(C) {
return function_B(currentIndex, C)
}
and then connect to this function:
item_A.signal_A.connect(where.ever.wrapFunctionB)
If the place where you connect has access to all parameters, you can also connect it to an anonymous function:
item_A.signal_A.connect(function(C) { function_B(from.where.ever.currentItem, C) })
passing an additional parameter to a function when function is called via event i.e :Connect()
You can use a higher-order function that is called with portal
and then returns a function which can use the parameter from the enclosing function:
local function portalTouched(portal)
return function(part)
-- We can use portal in this function
local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
if not HRP then return end
local DestinationName = portal:FindFirstChildOfClass("Attachment").Name
local Destination = script.Parent:FindFirstChild(DestinationName):FindFirstChild(portal.Name)
HRP.CFrame = Destination.WorldCFrame
end
end
for _,portal in pairs(script.Parent:GetChildren())do
if portal:IsA("Script") then continue end
portal.Touched:Connect(portalTouched(portal))
end
Passing extra arguments to callback function
You can use bind()
to pre-define an extra argument to the callback function:
...
sub.on('message', onSubMessage.bind(sub, socket));
...
function onSubMessage(socket, channel, message) {
if (channel === 'chat') socket.send(message);
}
This does create a new function instance for every new connection, so there is probably not a real upside to using a wrapping function in terms of memory usage.
Pass extra arguments to PyQt slot without losing default signal arguments
Your lambda
could take an argument:
def connections(self):
my_button.clicked.connect(lambda checked: self.on_button(checked, 'hi'))
def on_button(self, checked, message):
print checked, message # prints "True, hi"
Or you could use functools.partial
:
# imports the functools module
import functools
def connections(self):
my_button.clicked.connect(functools.partial(self.on_button, 'hi'))
def on_button(self, message, checked):
print checked, message # prints "True, hi"
python pyQt4 listWidget passing extra arguments
You should use it as follows:
extra_argument = {your value}
listObj.itemClicked.connect(lambda item, extra_argument= extra_argument: self.some_function(item, extra_argument))
def some_function(self, item, extra_argument):
[...]
Or you can use partial but the default argument should be at the end:
listObj.itemClicked.connect(partial(self.some_function, extra_argument))
def some_function(self, extra_argument, item):
[...]
Pass an extra argument to a callback function
Just create a function(magic) {}
as a wrapper callback:
callWithMagic(function(magic) {
return processMagic(magic, 42);
});
Or using ECMAScript 6: arrow functions:
callWithMagic(magic => processMagic(magic, 42));
Python: Pass extra arguments to callable
You just need a function that wraps cost
. A quick way is to use functools.partial
.
import functools
def cost(i, j, d, value1, value2):
'Example function'
return i, j, d, value1, value2
# This makes a new function which takes all parameters of `cost` except those
# you've already passed. Here I'm going to set value1 and value2 to 1 and 2
# respectively.
cost_partial = functools.partial(cost, value1=1, value2=2)
# A new function that only accepts the 3 args i, j, d
cost_partial('i', 'j', 'd') # --> ('i', 'j', 'd', 1, 2)
# use as
path = nx.dijkstra_path(graph, src, dst, weight=cost_partial)
Related Topics
How to Create a Trie in Python
How to Use Python Requests to Fake a Browser Visit A.K.A and Generate User Agent
Using 'Try' VS. 'If' in Python
Importing Orange Returns "Importerror: No Module Named Orange"
Executable Python Program with All Dependencies for Linux
Set Bash Variable from Python Script
How to Get Method Parameter Names
How to Efficiently Compare Two Unordered Lists (Not Sets)
Sending "User-Agent" Using Requests Library in Python
Run Linux Grep Command from Python Subprocess
I Have a Problem with Sending Mail:Typeerror: _Init_() Got an Unexpected Keyword Argument 'Context'
Python Dictionary: Are Keys() and Values() Always the Same Order
Python None Comparison: Should I Use "Is" or ==