【问题标题】:Python can't use values from constructorPython 不能使用构造函数中的值
【发布时间】:2020-06-08 21:52:27
【问题描述】:

我在使用构造函数中的值时遇到问题。我正在为我的模糊逻辑传递三个(输入)值,由于某种原因我无法使用。我收到一条错误消息:name 'self' is not defined

我不知道我做错了什么,因为我的代码中有一个类似的类并且工作正常?这只是我的代码的一个简单版本:

class FirstFuzzy:

    def __init__(self, length, arr, dep):
        self.veh_que = length
        self.veh_arr = arr
        self.veh_dep = dep

    print(self.veh_que)  

【问题讨论】:

    标签: python-3.x methods constructor self


    【解决方案1】:

    请在构造函数中移动打印语句。或者在类中创建一个方法,然后尝试打印该语句。

    类FirstFuzzy:

    def __init__(self, length, arr, dep):
        self.veh_que = length
        self.veh_arr = arr
        self.veh_dep = dep
        print(...)
    

    【讨论】:

    • 所以我只能在 init 内部使用这些值还是在外部使用其他方法?
    • 是的。您不能引用范围之外的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2022-06-13
    • 2016-09-02
    • 2015-12-22
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多