Python novice asks questions about attributes in classes
欧阳克
欧阳克 2017-06-15 09:21:38
0
1
665

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.

欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all (1)
给我你的怀抱
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 theselfobject. It doesn’t just have to be written.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!