简体   繁体   English

Python 类( AttributeError: '' object has no attribute '')

[英]Python Classes ( AttributeError: '' object has no attribute '')

Having trouble understanding the problem in my code, new to classes (generally python too, so sorry if I name things wrong).无法理解我的代码中的问题,新的类(通常也是 python,如果我命名错误,很抱歉)。 I receive this error:我收到此错误:

I think my code is too long winded to include in here, so I made a simplified version to test the concept below.我认为我的代码过于冗长,无法包含在此处,因此我制作了一个简化版本来测试下面的概念。

The question is, how can I create a new self object "self4"?问题是,我怎样才能创建一个新的自我对象“self4”? Which would then be available to other functions within the class.然后可用于类中的其他函数。 Currently I get this error.目前我收到此错误。

AttributeError: 'className' object has no attribute 'self4' AttributeError: 'className' 对象没有属性 'self4'

class className(object):

   def __init__(self, self1=1,self2=2,self3=3):
        self.self1=self1
        self.self2=self2
        self.self3=self3

   def evaluate(self, self5):
        print className.func1(self) + className.func2(self)
        self.self5=self5
        print className.func1(self)

   def func1(self):
       return self.self1 + self.self5

   def func2(self):
       self.self4 = self.self1+self.self2+self.self3
       return self.self4

filename tester.py文件名tester.py

import tester.py

mst=tester.className()

mst.evaluate()

Edit:编辑:
Your code works fine!你的代码工作正常!
What is the Problem?问题是什么?

I still think it is better to move self4 into the init!我还是觉得把self4移到init里比较好!

Original原来的
I think the most logical thing would be to define self4 on init:我认为最合乎逻辑的事情是在 init 上定义self4

class className(object):
    def __init__(self, self1=1, self2=2, self3=3):
        self.self1 = self1
        self.self2 = self2
        self.self3 = self3
        self.self4 = None

   #rest of class

If anyone still has this issue: you get this error when your indentation is goofed.To fix the asked question above, you just have to add a space before the last two functions definitions, that is;如果有人仍然有这个问题:当你的缩进错误时,你会得到这个错误。要解决上面的问题,你只需要在最后两个函数定义之前添加一个空格,即; class className(object):类类名(对象):

def __init__(self, self1=1,self2=2,self3=3):
    self.self1=self1
    self.self2=self2
    self.self3=self3

def evaluate(self, self5):
    
    print className.func1(self) + className.func2(self)
    self.self5=self5
    print className.func1(self)

def func1(self):
    return self.self1 + self.self5

def func2(self):
    self.self4 = self.self1+self.self2+self.self3
    return self.self4

just make sure they all have similar indentation, and you are good to go.只要确保它们都有相似的缩进,你就可以开始了。

你应该在方法中传递self4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于类的新手python问题:AttributeError:'list'对象没有属性 - Newbie python question about classes: AttributeError: 'list' object has no attribute AttributeError:object 没有属性 matplot tk 和类 python - AttributeError: object has no attribute matplot tk and classes python Python Tkinter: AttributeError: 'str' object 没有属性 '_root'(使用类) - Python Tkinter: AttributeError: 'str' object has no attribute '_root' (Using Classes) Python AttributeError: Object 没有属性 - Python AttributeError: Object has no attribute AttributeError:“ MultiOutputClassifier”对象没有属性“ classes_” - AttributeError: 'MultiOutputClassifier' object has no attribute 'classes_' AttributeError:'LinearSVC'对象没有属性'classes_' - AttributeError: 'LinearSVC' object has no attribute 'classes_' AttributeError: 'NumpyArrayIterator' object 没有属性 'classes' - AttributeError: 'NumpyArrayIterator' object has no attribute 'classes' IronPython - 带有自定义类的“AttributeError:object has no attribute” - IronPython - “AttributeError: object has no attribute” with custom classes Python 类('' 对象没有属性 '') - Python Classes ('' object has no attribute '') python错误 - attributeError:'str'对象没有属性 - python error - attributeError: 'str' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM