Home  >  Q&A  >  body text

Python novice asks questions about attributes in classes

code show as below:

# -*- coding:gb2312 -*-

class Home:
    def __init__(self,new_name,new_area,new_addr,new_info):
        name = new_name
        area = new_area
        addr = new_addr
        info = new_info
    def __str__(self):
        return ("房子的户主是:%s,面积是:%d平米,地址是:%s,户型是:%s。"%(self.name,self.area,self.addr,self.info))


class Bed:
    def __init__(self):
        pass
    def __str__(self):
        pass

fangzi = Home("谢霆锋",182,"香港","三室一厅")
print(fangzi)

operation result:

My question:
The system prompts: return ("The owner of the house is: %s, the area is: %d square meters, the address is: %s, and the house type is: %s." %(self.name,self.area,self.addr,self.info))
She said that the attribute name cannot be found in this paragraph, but I clearly wrote it in init. I checked it inside and out many times but still can't figure it out.

欧阳克欧阳克2654 days ago686

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-15 09:23:38

    def __init__(self,new_name,new_area,new_addr,new_info):
            self.name = new_name
            self.area = new_area
            self.addr = new_addr
            self.info = new_info
            

    You need to add attributes to the self object. It doesn’t just have to be written.

    reply
    0
  • Cancelreply