0

Learning python and the author did not show the part of printing the output of Class method. The author only wanted to demonstrate where we can use Map function.

But I'm trying to figure out how to print what it has returned.

class User:
  def __init__(self, username, password):
    self.username = username
    self.password = password
  
  @classmethod
  def from_dict(cls, data):
    return cls(data['username'], data['password'])


users = [
  { 'username': 'rolf', 'password': '123'},
  { 'username': 'teclado', 'password': '1234'}
]


user = [User.from_dict(user) for user in users]
print(user)

OR printing this to terminal

user = map(User.from_dict, users)
print(user)

It prints a reference. I want to see the data actually.

[<__main__.User object at 0x7f54b5e02b80>, <__main__.User object at 0x7f54b5e02d00>]
<map object at 0x7f54b5e02d60>
user630702
  • 2,529
  • 5
  • 35
  • 98

2 Answers2

1

Either:

  1. print the attributes directly to bypass the fairly useless default repr:

    print(user.username, user.password)
    

    or...

  2. Define a __repr__ for the class so that it print(user) (or just echoing in the REPL) has a useful string form to display:

     class User:
         # ... rest of class ...
         def __repr__(self):
             return '{0.__class__.__name__}({0.username!r}, {0.password!r})'.format(self)
    
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

Option 1: Implement str() or repr() in the class's metaclass

This option is preferable.

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    @classmethod
    def from_dict(cls, data):
        return cls(data['username'], data['password'])

    def __str__(self):
        return f'username : {self.username}, password : {self.password}'

    def __repr__(self):
        return f'User(username={self.username}, password={self.password})'

users = [
    {'username': 'rolf', 'password': '123'},
    {'username': 'teclado', 'password': '1234'}
]

user = [User.from_dict(user) for user in users]
# [username : rolf, password : 123, username : teclado, password : 1234]
print([u for u in user])

Option 2 : Using class __dict__

Only if you cannot change the original class.

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    @classmethod
    def from_dict(cls, data):
        return cls(data['username'], data['password'])

users = [
    {'username': 'rolf', 'password': '123'},
    {'username': 'teclado', 'password': '1234'}
]

user = [User.from_dict(user) for user in users]
# [{'username': 'rolf', 'password': '123'}, {'username': 'teclado', 'password': '1234'}]
print([u.__dict__ for u in user])
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
  • 1
    Your implementation of `__str__` and `__repr__` is backwards; the `repr` is typically supposed to produce something you could `eval` to reproduce the instance, while the `str` is the "human-friendly" version; your code reverses that relationship. – ShadowRanger Sep 06 '20 at 12:05