如何在同一类中的Python中调用另一个函数?

huangapple go评论99阅读模式
英文:

How can function call another functions in same class python

问题

  1. 我想创建一个包含一些函数的类其中一个函数在类中调用另一个函数类似这样
  2. ```python
  3. import pandas as pd
  4. class Prep:
  5. def __init__(self, data):
  6. self.data = data
  7. def slicing(self):
  8. sliceInput = self.data.iloc[:, 1:8]
  9. sliceTarget = self.data.iloc[:, 8]
  10. return sliceInput, sliceTarget
  11. def convert(self):
  12. sliceInput, sliceTarget = self.slicing()
  13. convertInput = sliceInput.to_numpy(float)
  14. convertTarget = sliceTarget.to_numpy(int)
  15. return convertInput, convertTarget
  16. if __name__ == "__main__":
  17. data_frame = pd.read_csv('data_manual.csv', sep=';')
  18. tes = Prep(data_frame)
  19. print(tes.convert())

我得到了以下错误:
NameError: name 'sliceInput' is not defined

如何在函数中调用 convertInputconvertTarget
为什么我会得到这个错误,我完全不理解。

  1. <details>
  2. <summary>英文:</summary>
  3. I want to create a class that includes some functions, that one of them call another one in class, something like that:

import pandas as pd

class Prep:
def init(self, data):
self.data = data

  1. def slicing(self):
  2. sliceInput = self.data.iloc[:, 1:8]
  3. sliceTarget = self.data.iloc[:, 8]
  4. return sliceInput, sliceTarget
  5. def convert(self):
  6. convertInput = sliceInput.to_numpy(float)
  7. convertTarget = sliceTarget.to_numpy(int)
  8. return convertInput, convertTarget

if name == "main":
data_frame = pd.read_csv('data_manual.csv', sep=';')

  1. tes = Prep(data_frame)
  2. print(tes.convert())
  1. i got error like this
  2. `NameError: name &#39;sliceInput&#39; is not defined`
  3. how to call ` convertInput ` and `convertTarget` in function
  4. why I get an error, I don&#39;t understand at all.
  5. </details>
  6. # 答案1
  7. **得分**: 2
  8. 你需要在`convert`函数中添加`sliceInput, sliceTarget = self.slicing()`
  9. ```python
  10. import pandas as pd
  11. class Prep:
  12. def __init__(self, data):
  13. self.data = data
  14. def slicing(self):
  15. sliceInput = self.data.iloc[:, 1:8]
  16. sliceTarget = self.data.iloc[:, 8]
  17. return sliceInput, sliceTarget
  18. def convert(self):
  19. sliceInput, sliceTarget = self.slicing()
  20. convertInput = sliceInput.to_numpy(float)
  21. convertTarget = sliceTarget.to_numpy(int)
  22. return convertInput, convertTarget
  23. if __name__ == "__main__":
  24. data_frame = pd.read_csv('data_manual.csv', sep=';')
  25. tes = Prep(data_frame)
  26. print(tes.convert())
英文:

You need to add sliceInput, sliceTarget = self.slicing() in covert

  1. import pandas as pd
  2. class Prep:
  3. def __init__(self, data):
  4. self.data = data
  5. def slicing(self):
  6. sliceInput = self.data.iloc[:, 1:8]
  7. sliceTarget = self.data.iloc[:, 8]
  8. return sliceInput, sliceTarget
  9. def convert(self):
  10. sliceInput, sliceTarget = self.slicing()
  11. convertInput = sliceInput.to_numpy(float)
  12. convertTarget = sliceTarget.to_numpy(int)
  13. return convertInput, convertTarget
  14. if __name__ == &quot;__main__&quot;:
  15. data_frame = pd.read_csv(&#39;data_manual.csv&#39;, sep=&#39;;&#39;)
  16. tes = Prep(data_frame)
  17. print(tes.convert())

答案2

得分: 2

只需将self添加到已定义的属性中。
此外,因为在实例化Prep对象后,data是不可变的,所以最好在__init__()方法中只初始化切片变量一次。

  1. import pandas as pd
  2. class Prep:
  3. def __init__(self, data):
  4. self.data = data
  5. self.sliceInput = data.iloc[:, 1:8]
  6. self.sliceTarget = data.iloc[:, 8]
  7. def slicing(self):
  8. return self.sliceInput, self.sliceTarget
  9. def convert(self):
  10. convertInput = self.sliceInput.to_numpy(float)
  11. convertTarget = self.sliceTarget.to_numpy(int)
  12. return convertInput, convertTarget
  13. if __name__ == "__main__":
  14. data_frame = pd.read_csv('data_manual.csv', sep=';')
  15. tes = Prep(data_frame)
  16. print(tes.convert())

注意:代码中的if __name__ == "__main__": 已被更正为正常的Python语法。

英文:

Just add self to the defined attributes.
Also, because data is immutable after you have instantiated a Prep object, it would be better for performance if you just initialize the slice variables once in the __init__() method.

  1. import pandas as pd
  2. class Prep:
  3. def __init__(self, data):
  4. self.data = data
  5. self.sliceInput = data.iloc[:, 1:8]
  6. self.sliceTarget = data.iloc[:, 8]
  7. def slicing(self):
  8. return self.sliceInput, self.sliceTarget
  9. def convert(self):
  10. convertInput = self.sliceInput.to_numpy(float)
  11. convertTarget = self.sliceTarget.to_numpy(int)
  12. return convertInput, convertTarget
  13. if __name__ == &quot;__main__&quot;:
  14. data_frame = pd.read_csv(&#39;data_manual.csv&#39;, sep=&#39;;&#39;)
  15. tes = Prep(data_frame)
  16. print(tes.convert())

huangapple
  • 本文由 发表于 2020年1月4日 12:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定