import random import fitness class optimizing: def __int__(self): self.finess_data=fitness.fitness() self.a=1 print(self.a) def p(self): print(self.a) data=optimizing() data.p() 报错如何解决:C:\Users\z84259074\AppData\Local\Programs\Python\Python310\python310.exe D:/Users/z84259074/PycharmProjects/参数自优化/self_optimizing.py Traceback (most recent call last): File "D:\Users\z84259074\PycharmProjects\参数自优化\self_optimizing.py", line 12, in <module> data.p() File "D:\Users\z84259074\PycharmProjects\参数自优化\self_optimizing.py", line 9, in p print(self.a) AttributeError: 'optimizing' object has no attribute 'a'
时间: 2023-09-14 16:12:57 浏览: 85
这个错误是因为在 `__int__()` 方法中,你使用了双下划线 `__` 代替了双下划线 `__init__()` 方法的名称。在 Python 中,类的构造方法应该被命名为 `__init__()`,而不是 `__int__()`。因此,你需要将 `__int__()` 改为 `__init__()`。
另外,你还需要将 `self.a=1` 改为 `self.a = 1`,确保在初始化时正确地给实例变量 `a` 赋值。
修改后的代码如下:
```python
import random
import fitness
class optimizing:
def __init__(self):
self.finess_data = fitness.fitness()
self.a = 1
print(self.a)
def p(self):
print(self.a)
data = optimizing()
data.p()
```
这样就可以正确地初始化 `optimizing` 类的实例对象并调用 `p()` 方法了。
相关问题
import random import fitness class optimizing(): def __int__(self): self.finess_data=fitness.fitness() #print(self.finess_data) def p(self): print(self.finess_data) data=optimizing data.p() 报错
你在创建 `optimizing` 类的实例时,忘记了在类名后面加上括号,导致实例化失败。你需要将 `data=optimizing` 改为 `data=optimizing()`,这样才能正确地创建 `optimizing` 类的实例对象。然后再调用 `data.p()` 方法就不会报错了。请注意,类名后面必须加上括号,以表示实例化该类。
bs_data = w_data[w_data['波束场景No'] == cross_data['波束场景No'][0]] 报错:Traceback (most recent call last): File "C:\Users\z84259074\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3652, in get_loc return self._engine.get_loc(casted_key) File "pandas\_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: '波束场景No' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\Users\z84259074\PycharmProjects\参数自优化\self_optimizing.py", line 127, in <module> data = optimizing() File "D:\Users\z84259074\PycharmProjects\参数自优化\self_optimizing.py", line 18, in __init__ self.optimizing_main() File "D:\Users\z84259074\PycharmProjects\参数自优化\self_optimizing.py", line 120, in optimizing_main self.child2=self.mutation_cdata(fitness_data,self.cross_data) File "D:\Users\z84259074\PycharmProjects\参数自优化\self_optimizing.py", line 86, in mutation_cdata bs_data = w_data[w_data['波束场景No'] == cross_data['波束场景No'][0]] File "C:\Users\z84259074\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3761, in __getitem__ indexer = self.columns.get_loc(key) File "C:\Users\z84259074\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3654, in get_loc raise KeyError(key) from err KeyError: '波束场景No'
根据错误信息,看起来在使用`w_data[w_data['波束场景No'] == cross_data['波束场景No'][0]]`时出现了`KeyError: '波束场景No'`错误。这个错误通常表示在`w_data` DataFrame中不存在名为'波束场景No'的列。
要决这个问题,你可以先检查一下`w_data` DataFrame的列是否与你使用的列名完全配。请确保列名的拼写和大小写与数据框中的实际列名一致。
你可以使用`columns`属性来查看`w_data` DataFrame的列名:
```python
print(w_data.columns)
```
确保列名正确拼写和大小写匹配后,再次尝试运行`w_data[w_data['波束场景No'] == cross_data['波束场景No'][0]]`应该就不会报错了。
阅读全文