Python variable gives me the name of the user then changes to all its info

I’m coding a Discord bot in Python, and I’m trying to get a user name by his ID. So this is what I have:

ok = ctx.message.guild.get_member(313628621841498114) 
print(ok) #prints Thomsd#4688

This is indeed what I’m trying to get. However, when I do

dict_temp = {"membres":ok}
print(dict_temp)

I get:

{'membres': <Member id=313628621841498114 name='Thomsd' discriminator='4688' bot=False nick=None guild=<Guild id=968274297200197692 name='LE machin de la RATP' shard_id=None chunked=True member_count=9>>}

Is there any reason why it appears and does anyone know how I can fix it to add to the dictionary the first result? Thanks

>Solution :

The print() function calls str(pl) to get the string to print. The discord.Member class has a __str__() method that returns name#discriminator, so that’s what is printed.

When you print the dictionary, it calls str(dict_temp). The __str()__ method of dictionaries calls repr() on the values in the dictionary. The default __repr__() method of classes returns a string as you show, with the class name followed by all the attributes. discord.Member() hasn’t overridden this to return something more succinct.

list.__repr__() is similar. You’d see the same thing if you did print([ok])

Leave a Reply