Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading