python:AttributeError:类没有属性‘***’

抽搐人偶师Orz 抽搐人偶师Orz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
初学python遇到了问题
按书上的代码敲的结果有BUG

显示这个File "D:/PycharmProjects/chapter9/electric_car.py", line 52, in <module>
my_tesla.battery.describe_battery()
AttributeError: 'ElectricCar' object has no attribute 'battery'


class Car():

def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()

def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")

def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll backk an odometer!")

def increment_odometer(self, miles):
self.odometer_reading += miles


class Battery():
"""一次模拟电动汽车电瓶的简单尝试"""

def _init_(self, battery_size=70):
"""初始化电瓶属性"""
self.battery_size = battery_size

def describe_battery(self):
"""打印一条描述电瓶容量的消息"""
print("This car has a " + str(self.battery_size) + "-kWh battery.")


class ElectricCar(Car):
"""电动汽车的独特之处"""

def _init_(self, make, model, year):
"""
电动汽车的独特之处
初始化父类的属性,再初始化电动汽车持有的属性
"""
super()._init_(make, model, year)
self.battery = Battery()


my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
...全文

相关推荐

【Python】AttributeError: ‘AutoSchema’ object has no attribute ‘get_link’

【问题描述】        今天在搭建django-rest-swagger的时候出现了如下错误: Traceback (most recent call last): File "D:\anaconda\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "D:\anaconda\lib\site-packages\django\core\handlers\base.py", line 115, in _ge

weixin_38534444

# Pycharm关于AttributeError: ‘DataFrame’ object has no attribute ‘score’的错误

Pycharm关于AttributeError: ‘DataFrame’ object has no attribute ‘score’的错误 import pandas data = pandas.read_excel( r"C:\Users\ASUS\Desktop\0012\data7.1.2.xlsx", ) data.score.describe() # 逐项分析各统计量 data.score.size data.score.max() data.score.min() data.score.sum() data.score.mea

weixin_38720756

谈谈Python:为什么类中的私有属性可以在外部赋值并访问

主要介绍了谈谈Python:为什么类中的私有属性可以在外部赋值并访问,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

weixin_38684892

python错误:AttributeError: ‘module’ object has no attribute ‘setdefaultencoding’问题的解决方法

Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8 查看目前系统字符集 复制代码 代码如下: import sys print sys.getdefaultencoding() 执行: 复制代码 代码如下: [root@lee ~]# python a.py ascii 修改成utf-8 复制代码 代码如下: import sys   sys.setdefaultencoding(‘utf-8’)   print sys.getdefaultencoding() 执行: 复制代码 代码如下: [root@lee ~]# py

weixin_38744153

python的mysql数据查询及报错AttributeError: ‘Connection’ object has no attribute ‘curson’

import pymysql #创建连接 con = pymysql.connect(host='localhost',user='root',password='123456',port=3306,database='zhy') #创建游标对象 cur = con.curson() #编写查询的sql语句 sql = 'select * from t_student' try: cur.execute(sql) print(查询成功) students = cur.fetchall() print(students) except Exception as

weixin_38725015
评论 7
收藏 已收藏
分享
返回顶部
抽搐人偶师Orz
脚本语言 37,738 成员 34,208 内容