Reputation: 159
I researched online and still am having a hard time understanding how to create classes. The examples in SoloLearn, DataCamp, and other sites often use the Person
/ Employee
example, such as this one:
class Person:
def __init__(self, first, last):
self.firstname = first
self.lastname = last
def Name(self):
return self.firstname + " " + self.lastname
class Employee(Person):
def __init__(self, first, last, staffnum):
Person.__init__(self,first, last)
self.staffnumber = staffnum
def GetEmployee(self):
return self.Name() + ", " + self.staffnumber
x = Person("Marge", "Simpson")
y = Employee("Homer", "Simpson", "1007")
print(x.Name())
print(y.GetEmployee())
I am trying to create an example class that returns a dictionary by modifying it slightly. Here is the code:
class Place:
def __init__(self, country, province):
self.country= country
self.province = province
def CountryState(self):
return {
'Province': self.province,
'Country': self.country
}
class Location(Place):
def __init__(self, country, province, city, street, postal):
super(Place, self).__init__(country, province, city, street, postal)
self.city = city
self.street = street
self.postal = postal
def GetLocation(self):
return CountryState().update({
'City': self.city,
'Street': self.street,
'Postal': self.postal
})
if __name__=="__main__":
x = Place('United States', "California")
y = Location("United States", "California", "Los Angeles", "Johnson Street", "90007")
print(x.CountryState())
print(y.GetLocation())
I get a Traceback as follows using pdb.set_trace()
:
Traceback (most recent call last):
File "/home/john/Python/class-example.py", line 37, in <module>
y = Location("United States", "California", "Los Angeles", "Johnson Street", "90007")
File "/home/john/Python/class-example.py", line 22, in __init__
super(Place, self).__init__(country, province, city, street, postal)
TypeError: object.__init__() takes no parameters
> /usr/lib/python3.5/idlelib/run.py(370)runcode()
-> jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>")
pdb
list
:
365 self.usr_exc_info = sys.exc_info()
366 if quitting:
367 exit()
368 # even print a user code SystemExit exception, continue
369 print_exception()
370 -> jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>")
371 if jit:
372 self.rpchandler.interp.open_remote_stack_viewer()
373 else:
374 flush_stdout()
375
I haven't been able to get __init__
to accept parameters. Please let me know what I am missing so I can return the dictionary from these lines:
print(x.CountryState())
print(y.GetLocation())
{ 'Country': 'United States', 'State': 'California' }
{ 'Country': 'United States', 'City': 'Los Angeles', 'Street':'Johnson Street', 'Postal Code': '90007'}
Upvotes: 0
Views: 3112
Reputation: 29071
In the Location(Place)
class you are calling
super(Place, self).__init__(country, province, city, street, postal)
You should call the super-class of the Location
class. Now, you are calling the super class of Place
. Since Place
has no declared superclass, it derives from object
class (which is the parent of all classes), and you are calling the object
constructor, which takes no parameters.
Also, even if you fix that, you are giving more parameters than Place
takes. The correct form would be
super(Location, self).__init__(country, province)
Upvotes: 1