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

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

How can function call another functions in same class python

问题

我想创建一个包含一些函数的类其中一个函数在类中调用另一个函数类似这样

```python
import pandas as pd

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

    def slicing(self):
        sliceInput = self.data.iloc[:, 1:8]
        sliceTarget = self.data.iloc[:, 8]
        return sliceInput, sliceTarget

    def convert(self):
        sliceInput, sliceTarget = self.slicing()
        convertInput = sliceInput.to_numpy(float)
        convertTarget = sliceTarget.to_numpy(int)
        return convertInput, convertTarget

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

    tes = Prep(data_frame)
    print(tes.convert())

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

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


<details>
<summary>英文:</summary>

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

def slicing(self):
    sliceInput = self.data.iloc[:, 1:8]
    sliceTarget = self.data.iloc[:, 8]
    return sliceInput, sliceTarget

def convert(self):
    convertInput = sliceInput.to_numpy(float)
    convertTarget = sliceTarget.to_numpy(int)
    return convertInput, convertTarget

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

tes = Prep(data_frame)
print(tes.convert())
i got error like this
`NameError: name &#39;sliceInput&#39; is not defined`

how to call ` convertInput ` and `convertTarget` in function
why I get an error, I don&#39;t understand at all.

</details>


# 答案1
**得分**: 2

你需要在`convert`函数中添加`sliceInput, sliceTarget = self.slicing()`。

```python
import pandas as pd

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

    def slicing(self):
        sliceInput = self.data.iloc[:, 1:8]
        sliceTarget = self.data.iloc[:, 8]

        return sliceInput, sliceTarget

    def convert(self):
        sliceInput, sliceTarget = self.slicing()
        convertInput = sliceInput.to_numpy(float)
        convertTarget = sliceTarget.to_numpy(int)
        return convertInput, convertTarget

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

    tes = Prep(data_frame)
    print(tes.convert())
英文:

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

import pandas as pd


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

    def slicing(self):
        sliceInput = self.data.iloc[:, 1:8]
        sliceTarget = self.data.iloc[:, 8]

        return sliceInput, sliceTarget

    def convert(self):
        sliceInput, sliceTarget = self.slicing()
        convertInput = sliceInput.to_numpy(float)
        convertTarget = sliceTarget.to_numpy(int)
        return convertInput, convertTarget

if __name__ == &quot;__main__&quot;:
    data_frame = pd.read_csv(&#39;data_manual.csv&#39;, sep=&#39;;&#39;)

    tes = Prep(data_frame)
    print(tes.convert())

答案2

得分: 2

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

import pandas as pd

class Prep:
    def __init__(self, data):
        self.data = data
        self.sliceInput = data.iloc[:, 1:8]
        self.sliceTarget = data.iloc[:, 8]

    def slicing(self):
        return self.sliceInput, self.sliceTarget

    def convert(self):
        convertInput = self.sliceInput.to_numpy(float)
        convertTarget = self.sliceTarget.to_numpy(int)
        return convertInput, convertTarget

if __name__ == "__main__":
    data_frame = pd.read_csv('data_manual.csv', sep=';')
    
    tes = Prep(data_frame)
    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.

import pandas as pd


class Prep:
    def __init__(self, data):
        self.data = data
        self.sliceInput = data.iloc[:, 1:8]
        self.sliceTarget = data.iloc[:, 8]

    def slicing(self):
        return self.sliceInput, self.sliceTarget


    def convert(self):
        convertInput = self.sliceInput.to_numpy(float)
        convertTarget = self.sliceTarget.to_numpy(int)
        return convertInput, convertTarget

if __name__ == &quot;__main__&quot;:
    data_frame = pd.read_csv(&#39;data_manual.csv&#39;, sep=&#39;;&#39;)

    tes = Prep(data_frame)
    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:

确定