oop - Trying to Increment a variable on its inheritance python -


hi there new oop , python, trying increment user id variable child class, when create instance of parent class using inheritance doesn't seem recognise id variable parent class. example here

class user:     _id = 0     def __init__(self, name):         self.name = name         self.id = self._id         self.__class__._id += 1  class customer(user):     def __init__(self, name):      def lastname(self):         return "self.name.split()[-1]" 

if able access class attribute

>> chris = user("christopher allan") >> chris.id >> 0 

when try run

>> andy = customer('andy smith') >> andy.id   >>  traceback (most recent call last):     file "<pyshell#83>", line 1, in <module>     andy.id     attributeerror: 'customer' object has no attribute 'id' 

update

i completed rest of customer class cause of code not working me, sorry people used pass before briefness of question didn't test work pass in customer class.

customer not have id attribute base class (user) never has __init__ method called, , not otherwise defining it. since overriding base class' __init__ method need call super inherited code called:

class customer(user):     def __init__(self, name):         super(customer, self).__init__(name) 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -