AttributeError: Solution instance has no __call__ method ret = Solution().twoSum(param_1, param_2) Line 48 in _driver (Solution.py) _driver() Line 58 in <module> (Solution.py)
时间: 2024-02-07 20:02:11 浏览: 232
这个错误提示是因为你尝试以函数的方式调用 `Solution` 类的对象实例,但是 Python 认为你的对象实例不支持 `__call__` 方法,因此无法以函数的方式调用。
要解决这个问题,你可以按照以下步骤进行操作:
1. 确保你的 `Solution` 类实现了 `twoSum()` 方法,并且该方法接受参数 `nums` 和 `target`。
2. 确保你正确创建了 `Solution` 类的对象实例。例如,你可以使用以下代码创建 `Solution` 类的对象实例:
```python
solution = Solution()
```
3. 确保你使用正确的语法调用 `twoSum()` 方法,并传递正确的参数。例如,如果你要传递一个列表 `nums` 和一个整数 `target`,那么你可以使用以下代码调用 `twoSum()` 方法:
```python
result = solution.twoSum(nums, target)
print(result)
```
这样就可以正确调用 `twoSum()` 方法并输出结果了。注意,在调用对象方法时,通常需要使用对象的实例来调用方法。因此,你需要首先创建 `Solution` 类的对象实例并将其用作方法调用的第一个参数。
相关问题
AttributeError: 'NoneType' object has no attribute 'val' l1.append(head.val) Line 13 in deleteDuplicates (Solution.py) ret = Solution().deleteDuplicates(param_1) Line 61 in _driver (Solution.py) _driver() Line 71 in <module> (Solution.py)
这个错误提示表明在执行 `l1.append(head.val)` 时, `head` 对象为 `None`,因此无法访问其属性 `val`。这通常发生在链表为空的情况下。你需要在访问 `head` 对象之前,首先检查 `head` 是否为 `None`,例如:
```
if head is None:
return l1
```
这是一个简单的空值检查,它将确保在链表为空时不会出现上述错误。你可以在 `deleteDuplicates` 函数的开头添加此行。
esp32 蓝牙启动流程_ESP32教你简单进行蓝牙串口调试
ESP32的蓝牙启动流程如下:
1. 初始化蓝牙协议栈
2. 配置蓝牙参数,如设备名称、服务UUID等
3. 注册蓝牙事件回调函数
4. 启动蓝牙广播
5. 开启蓝牙可见性
6. 等待连接
以下是一个简单的ESP32蓝牙串口调试的示例:
```C
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#define GATTS_TAG "GATTS_DEMO"
#define TEST_DEVICE_NAME "ESP32_BLE_UART"
#define TEST_MANUFACTURER_DATA_LEN 17
/* The max length of characteristic value. When the gatt client write or prepare write,
* the data length must be less than MAX_VALUE_LENGTH.
*/
#define MAX_VALUE_LENGTH 500
/* Declare global variable */
static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x4c, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD};
static uint8_t test_service_uuid128[32] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x13, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x14, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_service_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x01, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x01, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_char_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x02, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x02, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static esp_gatt_char_prop_t test_property = 0;
static uint8_t char1_str[] = {0x11,0x22,0x33};
static esp_attr_value_t gatts_demo_char1_val = {
.attr_max_len = MAX_VALUE_LENGTH,
.attr_len = sizeof(char1_str),
.attr_value = char1_str,
};
static uint16_t gatts_demo_handle_table[3];
/* Full Database Description - Used to add attributes into the database */
static const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB] =
{
// Service Declaration
[IDX_SVC] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
sizeof(test_service_uuid), sizeof(test_service_uuid), (uint8_t *)&test_service_uuid},
ESP_GATT_UUID_PRI_SERVICE,
ESP_GATT_PERM_READ,
sizeof(test_service_uuid),
sizeof(test_service_uuid),
(uint8_t *)&test_service_uuid,
0
},
/* Characteristic Declaration */
[IDX_CHAR_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&test_property},
ESP_GATT_UUID_CHAR_DECLARE,
ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,
CHAR_DECLARATION_SIZE,
(uint8_t *)&test_property,
0
},
/* Characteristic Value */
[IDX_CHAR_VAL_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_128, (uint8_t *)&test_char_uuid, ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH, sizeof(gatts_demo_char1_val), gatts_demo_char1_val.attr_value},
ESP_UUID_LEN_128,
ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH,
sizeof(gatts_demo_char1_val),
gatts_demo_char1_val.attr_value,
0
},
};
static esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true,
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0x00,
.manufacturer_len = TEST_MANUFACTURER_DATA_LEN,
.p_manufacturer_data = test_manufacturer,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = sizeof(test_service_uuid),
.p_service_uuid = test_service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(GATTS_TAG, "advertising start failed");
}
break;
default:
break;
}
}
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
switch (event) {
case ESP_GATTS_REG_EVT:
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
esp_ble_gap_config_adv_data(&adv_data);
break;
case ESP_GATTS_CREAT_ATTR_TAB_EVT:
if (param->add_attr_tab.status != ESP_GATT_OK){
ESP_LOGE(GATTS_TAG, "create attribute table failed, error code=0x%x", param->add_attr_tab.status);
}
else if (param->add_attr_tab.num_handle != HRS_IDX_NB) {
ESP_LOGE(GATTS_TAG, "create attribute table abnormally, num_handle (%d) \
doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB);
}
else {
ESP_LOGI(GATTS_TAG, "create attribute table successfully, the number handle = %d\n",param->add_attr_tab.num_handle);
memcpy(gatts_demo_handle_table, param->add_attr_tab.handles, sizeof(gatts_demo_handle_table));
esp_ble_gatts_start_service(gatts_demo_handle_table[IDX_SVC]);
}
break;
case ESP_GATTS_CONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT");
break;
case ESP_GATTS_DISCONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GATTS_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_WRITE_EVT");
break;
case ESP_GATTS_MTU_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
break;
case ESP_GATTS_CONF_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT");
break;
case ESP_GATTS_EXEC_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_EXEC_WRITE_EVT");
break;
case ESP_GATTS_START_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_START_EVT");
break;
case ESP_GATTS_STOP_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_STOP_EVT");
break;
case ESP_GATTS_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_OPEN_EVT");
break;
case ESP_GATTS_CANCEL_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CANCEL_OPEN_EVT");
break;
case ESP_GATTS_CLOSE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CLOSE_EVT");
break;
case ESP_GATTS_LISTEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_LISTEN_EVT");
break;
case ESP_GATTS_CONGEST_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONGEST_EVT");
break;
case ESP_GATTS_UNREG_EVT:
case ESP_GATTS_DELETE_EVT:
default:
break;
}
}
void app_main()
{
esp_err_t ret;
ESP_LOGI(GATTS_TAG, "ESP_BLUETOOTH_BLE example started.");
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
return;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
return;
}
ret = esp_ble_gatts_register_callback(gatts_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gatts_app_register(ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
return;
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret){
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
ret = esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, HRS_IDX_NB, ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "create attr table failed, error code = %x", ret);
}
}
```
在这个示例中,我们使用了ESP-IDF提供的蓝牙协议栈和GATT Server框架,实现了一个简单的GATT Server,并且开启了蓝牙广播和可见性,使得其他蓝牙设备可以扫描到并连接我们的设备。在连接建立后,我们可以向GATT Server中的特定Characteristic写入数据,也可以从特定Characteristic读取数据。
阅读全文