Reputation: 123
I write a simple lambda function to generate a random list of booleans with the following code:
randBinList = lambda n: [random()<0.5 for _ in range(n)]
This function works perfect, for example:
>>> randBinList(100)
[False, False, True, False, False, True, True, False, True, False, False, False, False, True, False, True, False, False, False, False, False, False, True, False, True, True, True, False, False, True, True, False, False, False, False, True, False, True, False, True, False, False, True, False, False, False, True, False, True, False, False, False, False, True, True, True, True, False, True, True, True, True, False, True, True, True, True, False, False, True, True, False, True, False, True, False, True, True, False, False, False, False, True, True, False, False, False, False, True, False, False, True, True, True, False, True, False, True, False, False]
However, when I tried to add this function to a new class I'm working on I received the following error:
return self.randBinList(num_of_features)
TypeError: <lambda>() takes 1 positional argument but 2 were given
I do not know why it says that tow arguments were given? Here is the code of my class. Thanks for your help.
import sys, csv, os, subprocess, time
from collections import deque, defaultdict
import random
from typing import List
class ScriptFeatures():
randBinList = lambda n: [random()<0.5 for _ in range(n)]
def get_random_individual (self):
num_of_features = len(self.d)
return self.randBinList(100)
Upvotes: 8
Views: 49126
Reputation: 1
You have to override the on_checkbox_Active
callback.
Register to the callback ->
self.testCheckBox.bind(active= self.on_checkbox_Active)
override ->
def on_checkbox_Active(self,checkboxInstance, isActive):
print('test CheckBox')
Upvotes: 0
Reputation: 13
Another way to make it work is to call the lambda as a class method, rather than an object method. Here is user2653663 's example with the fix:
import numpy as np
class A(object):
rand_bin_list = lambda n: [np.random.random() < 0.5 for _ in range(n)]
def __init__(self):
print(self.__class__.rand_bin_list(5))
The use of __class__
in the last line makes it work, because it avoids calling rand_bin_list()
as a member of self
.
Upvotes: 1
Reputation: 123
As an alternative to the answer above, I redefined randBinList
as a local function inside the method get_random_individual
, which also works.
from random import random
from typing import List
class ScriptFeatures():
def get_random_individual (self):
randBinList = lambda n: [random() < 0.5 for _ in range(n)]
num_of_features = len(self.d)
individual = randBinList(num_of_features)
return individual
Upvotes: 0
Reputation: 2948
This example reproduces your error.
import numpy as np
class A(object):
rand_bin_list = lambda n: [np.random.random() < 0.5 for _ in range(n)]
def __init__(self):
print(self.rand_bin_list(5))
The problem here is that when you construct a function within a class, and call it prefixed with self.
you need to include self as the first argument. So you can either add self
as an argument, or you can define the function in the __init__
. So either
class A(object):
rand_bin_list = lambda self, n: [np.random.random() < 0.5 for _ in range(n)]
def __init__(self):
print(self.rand_bin_list(5))
or
class A(object):
def __init__(self):
self.rand_bin_list = lambda n: [np.random.random() < 0.5 for _ in range(n)]
print(self.rand_bin_list(5))
Upvotes: 16