P
P
PeteDoherty2020-08-27 23:20:02
Python
PeteDoherty, 2020-08-27 23:20:02

Why does it show the contents of the cell instead of the dictionary data?

Guys, tell me why when printing out the data of the dictionary, instead of the data of the class, the interpreter gives out the data of the memory cell?

class Owners: 
    def __init__(self, name, phone_number, age):
        try:
            self.name = name if type(name) == 'str' else str(name)
            self.phone_number = phone_number if type(phone_number) == 'str' else str(phone_number)
            self.age = age if type(age) == 'int' else int(age)
        except Exception:
            print('Error Type')

    def __str__(self): 
        return ''.join(self.name + ' ' + self.phone_number + ' ' + str(self.age))

def new_owner(): 
  
  name = input('Имя: ' )
  telephone = input('Телефон: ')
  age = input('Возраст: ')
  owner = Owners(name, telephone, age)
  return owner
  
  
class Vehicle:
    def __init__(self, num, color, vehicle_type, health):
        self.num = num if type(num) == 'str' else str(num)
        self.color = color if type(color) == 'str' else str(color)
        self.vehicle_type = vehicle_type if type(vehicle_type) == 'str' else str(vehicle_type)
        self.health = health if type(health) == 'int' else int(health)
    
    def __str__(self): 
        return ''.join(self.num + ' ' + self.color + ' '  + self.vehicle_type + ' ' + str(self.health)) 
    
    def heal(self):
        self.health = 100


    def condition(self, damage):
        self.health -= damage
        
    
def new_vehicle(owner): 
    num = input('Номeр автомобиля: ' )
    color = input('Цвет автомобиля: ' )
    vehicle_type = input('Тип автомобиля: ')
    health = input('Состояние автомобиля (%): ')
    vehicle = Vehicle(num, color, vehicle_type, health)
    return vehicle
    

data_autowner = {}
 
def main(): 
  auto_owner = new_owner()
  auto_vehicle = new_vehicle(auto_owner)  
  data_autowner[auto_owner.name] = auto_owner, auto_vehicle
  print (data_autowner)
  
main()  </source>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sswwssww, 2020-08-28
@PeteDoherty

By the way:

self.name = name if type(name) == 'str' else str(name)

- you will always have False (unless the owner's name is str).
Correct option:
self.name = name if isinstance(name, str) else str(name)

And so, this check is not needed here at all, since in the new_owner function you use inputs everywhere that return strings.
Vehincle and new_vehicle have a similar situation.
Same way
return ''.join(self.name + ' ' + self.phone_number + ' ' + str(self.age))

write either like this:
return ' '.join((self.name, phone_number, str(self.age)))

either like this:
return self.name + ' ' + self.phone_number + ' ' + str(self.age)

or better like this:
return f'{self.name} {self.phone_number} {self.age}'

and you can also:
return '{0} {1} {2}'.format(self.name, self.phone_number, self.age)

Regarding the question itself,
data_autowner[auto_owner.name] = auto_owner, auto_vehicle
here auto_owner and auto_vehicle are class objects, since you overridden their __str__ methods - when printing the objects themselves, everything will be ok, but you put these objects into a dictionary, and then print CAM DICTIONARY (which means that repr will be applied to dictionary objects), so you get what you get. Those. if you do like this
data_autowner[auto_owner.name] = str(auto_owner), str(auto_vehicle)
then you get what you want.
But doing it this way is bad form (to put it mildly), it's better to redefine __repr__, just like you did with __str__.
def __repr__(self): 
        return ' '.join((self.name, self.phone_number, str(self.age)))

S
soremix, 2020-08-27
@SoreMix

1. It's strange that your join is done. It takes an iterable object, and concatenates everything through your string.
Why they folded the lines and put them there is not clear
2. It should return an object, because you are printing several classes at once, also in a tuple. Print just auto_owner or auto_vehicle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question