'child class' object has no attribute 'attribute_name'









up vote
0
down vote

favorite












I can not retrieve the value of a variable created only by the inherited child class.
Also, changing the value of a variable inherited from the parent class in the init of the child class does not apply.



Here's the parent class:



class Car():

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

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

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 back an odometer!")

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


And here's the child class:



class ElectricCar(Car):

def __init___(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

def describe_battery(self):
print(self.battery_size)


Now, if I try to run this code:



my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla)
print(my_tesla.describe_battery())


I get the following exception:



<class '__main__.ElectricCar'>:
'make': 'tesla', 'model': 'model s', 'year': 2016, 'odometer_reading': 0

AttributeError: 'ElectricCar' object has no attribute 'battery_size'


I have an oddmeter_reading variable whose value is 0 in the parent class.
I changed from child class to 100, but it did not apply.
Also, the variable battery_size, which is set only in the child class, is not created in init.



What's the problem? What am I missing?










share|improve this question









New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    You wrote __init__ with three underscores at the right, so you did not "patch" the constructor.
    – Willem Van Onsem
    Nov 10 at 12:15














up vote
0
down vote

favorite












I can not retrieve the value of a variable created only by the inherited child class.
Also, changing the value of a variable inherited from the parent class in the init of the child class does not apply.



Here's the parent class:



class Car():

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

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

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 back an odometer!")

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


And here's the child class:



class ElectricCar(Car):

def __init___(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

def describe_battery(self):
print(self.battery_size)


Now, if I try to run this code:



my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla)
print(my_tesla.describe_battery())


I get the following exception:



<class '__main__.ElectricCar'>:
'make': 'tesla', 'model': 'model s', 'year': 2016, 'odometer_reading': 0

AttributeError: 'ElectricCar' object has no attribute 'battery_size'


I have an oddmeter_reading variable whose value is 0 in the parent class.
I changed from child class to 100, but it did not apply.
Also, the variable battery_size, which is set only in the child class, is not created in init.



What's the problem? What am I missing?










share|improve this question









New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    You wrote __init__ with three underscores at the right, so you did not "patch" the constructor.
    – Willem Van Onsem
    Nov 10 at 12:15












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I can not retrieve the value of a variable created only by the inherited child class.
Also, changing the value of a variable inherited from the parent class in the init of the child class does not apply.



Here's the parent class:



class Car():

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

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

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 back an odometer!")

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


And here's the child class:



class ElectricCar(Car):

def __init___(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

def describe_battery(self):
print(self.battery_size)


Now, if I try to run this code:



my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla)
print(my_tesla.describe_battery())


I get the following exception:



<class '__main__.ElectricCar'>:
'make': 'tesla', 'model': 'model s', 'year': 2016, 'odometer_reading': 0

AttributeError: 'ElectricCar' object has no attribute 'battery_size'


I have an oddmeter_reading variable whose value is 0 in the parent class.
I changed from child class to 100, but it did not apply.
Also, the variable battery_size, which is set only in the child class, is not created in init.



What's the problem? What am I missing?










share|improve this question









New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I can not retrieve the value of a variable created only by the inherited child class.
Also, changing the value of a variable inherited from the parent class in the init of the child class does not apply.



Here's the parent class:



class Car():

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

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

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 back an odometer!")

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


And here's the child class:



class ElectricCar(Car):

def __init___(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
self.odometer_reading = 100
self.battery_size = 70

def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

def describe_battery(self):
print(self.battery_size)


Now, if I try to run this code:



my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla)
print(my_tesla.describe_battery())


I get the following exception:



<class '__main__.ElectricCar'>:
'make': 'tesla', 'model': 'model s', 'year': 2016, 'odometer_reading': 0

AttributeError: 'ElectricCar' object has no attribute 'battery_size'


I have an oddmeter_reading variable whose value is 0 in the parent class.
I changed from child class to 100, but it did not apply.
Also, the variable battery_size, which is set only in the child class, is not created in init.



What's the problem? What am I missing?







python python-3.x class attributes super






share|improve this question









New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 13:11









martineau

64.2k887170




64.2k887170






New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 12:12









Ssungnni

1




1




New contributor




Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ssungnni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    You wrote __init__ with three underscores at the right, so you did not "patch" the constructor.
    – Willem Van Onsem
    Nov 10 at 12:15












  • 2




    You wrote __init__ with three underscores at the right, so you did not "patch" the constructor.
    – Willem Van Onsem
    Nov 10 at 12:15







2




2




You wrote __init__ with three underscores at the right, so you did not "patch" the constructor.
– Willem Van Onsem
Nov 10 at 12:15




You wrote __init__ with three underscores at the right, so you did not "patch" the constructor.
– Willem Van Onsem
Nov 10 at 12:15

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Ssungnni is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238824%2fchild-class-object-has-no-attribute-attribute-name%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Ssungnni is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















Ssungnni is a new contributor. Be nice, and check out our Code of Conduct.












Ssungnni is a new contributor. Be nice, and check out our Code of Conduct.











Ssungnni is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238824%2fchild-class-object-has-no-attribute-attribute-name%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin