0

So I am currently writing a bot for discord, a texting app service with an api.

Fairly new to python so I'm just going to try and explain what I would like best.

I have a list of strings. I am trying to use a item from the list in a Class Object but am unsure how to convert the string to an object. like so.

list = ['!','display','display_name']

and I want to do this.

print(self.user.list[2])

how would I convert the list[2] to a object so that print statement can work?

  • 1
    What do you mean by a "class object"? Do you mean some instance of a user-defined class? Note, `str` objects *are objects*. You need to add some more details about what you are trying to do. So, ok, you've provided an example list, but what do you mean you want to do `self.user.list[2]`? What is `self.user` supposed to be? In any case, you just need to add a list as the `list` attribute to whatever `self.user` is supposed to be (some other instance of another class you've defined?) – juanpa.arrivillaga Sep 22 '20 at 15:07
  • `getattr(ctx.author, list[2])` or just `ctx.author.display_name` – Joshua Nixon Sep 22 '20 at 15:08
  • Does this answer your question? [Python string to attribute](https://stackoverflow.com/questions/3253966/python-string-to-attribute) – Joshua Nixon Sep 22 '20 at 15:08
  • I'm not sure what any of those are, like a class object or a user defined class. All I know is that self = discord.Client and self.user has parameters like "display_name" or "avatar_url". I already have a code where I can parse the command where I say something like !edit blah blah, and it would put blah blah into a list. I want to take the first blah, and be able to call that as such. Like self.user.blah, and it would return me whatever blah stands for in parameter – brandon carpenter Sep 22 '20 at 15:13

1 Answers1

0

You can try working with classes like this:

class User:
    def __init__(self, display, display_name):
        self.display = display
        self.display_name = display_name

first_user = User()
print(first_user.display_name)

second_user = User()
print(second_user.display)
Bhavye Mathur
  • 1,024
  • 1
  • 7
  • 25