Can’t find the role I just created in Discord.py – Why?

When I create a role, try to find it by name, store in a variable role and change the role’s place in the roles hierarchy, it appears that the variable has the value of None. That means it didn’t find the role in the guild, right? But I just created the role! Why is it happening?

await guild.create_role(name="Muted", colour=discord.Colour.red(), permissions=discord.Permissions(send_messages=False))
role = discord.utils.get(guild.roles, name="Muted")
await role.edit(position=3)

An error appears:

AttributeError: 'NoneType' object has no attribute 'edit'

Answer

You can save it into a variable from the beginning, this can also be done for any object (TextChannel, Message, etc..)

role = await guild.create_role(name="Muted",
                               colour=discord.Colour.red(),
                               permissions=discord.Permissions(send_messages=False))
await role.edit(position=3)