5

So say I have this class:

class Person:
    first_name = 'First Name'
    last_name = 'Last Name'
    phone_number = '000-000'

What I want to do is to get first_name, last_name and phone_number, in the exact order.
Thank you.

Norak
  • 448
  • 1
  • 9
  • 22
  • 2
    So, you can access the *members that belong to a class using* `vars(Person)`. However, you will find that this includes *all members*, so things like methods are members on a class, and so are other, special things like `__doc__` and `__dict__`. So, filter the `vars(Person)`, maybe for things that don't `startswith('_')`, and maybe things that are `not callable(...)` – juanpa.arrivillaga Aug 25 '17 at 17:41
  • Also, hopefully you aren't making `first_name`, `last_name`, and `phone_number` class-variables in your actual class - those should be instance variables. – juanpa.arrivillaga Aug 25 '17 at 17:41
  • If you know the attribute you're looking for, i.e. `first_name`, you can easily get that attribute with `Person.first_name`. But presumably you knew this already, so what is your _real_ question? – John Gordon Aug 25 '17 at 17:45
  • One point about the duplicate: everything applies to classes, since in Python, classes *themselves* are merely objects, and "static members" (a term not really used in Python) are *merely members of the class object itself*. Finally, you cannot guarantee order. You'll have to impose that order yourself. – juanpa.arrivillaga Aug 25 '17 at 17:45
  • @juanpa.arrivillaga the "only look at `__dict__` of the object, not whatever is inherited from the class" part of the answer will definitely fail. – o11c Aug 25 '17 at 17:55
  • 2
    @Norak To get the order, use a metaclass and have `__prepare__` return an `OrderedDict`, then store the order before `type()` converts it back to a regular dict. – o11c Aug 25 '17 at 17:56
  • @o11c sorry, I don't understand what you are trying to say. You will only get the members of the class, which is what you want, and nothing inherited form the metaclass... – juanpa.arrivillaga Aug 25 '17 at 17:57
  • @juanpa.arrivillaga then how the hell are you a gold-badge holder? Just because classes technically are objects doesn't mean they always behave the same. – o11c Aug 25 '17 at 17:59
  • @o11c I'm sorry, I am just not understanding what you are trying to communicate. I don't see where this: "only look at __dict__ of the object, not whatever is inherited from the class" is coming from. Not trying to be confrontational, only to understand. Are you saying that this will not give you *inherited* class members? I agree. – juanpa.arrivillaga Aug 25 '17 at 18:02
  • @juanpa.arrivillaga The duplicate you chose refers entirely to variables set on an *instance*. There is no (non-pedantic) instance here, so none of those answers will work for the OP's problem, which has variable in the class. – o11c Aug 25 '17 at 18:07
  • 3
    @o11c the answer is that you cannot do so reliably. But `[v for v, m in vars(Person).items() if not (v.startswith('_') or callable(m))]` works in this case. I don't think your suggested dupe is a correct answer, because my interpretation is that the OP wants a general solution for classes they may not have control over.If the OP says otherwise, I'd be happy to redirect the dupe. – juanpa.arrivillaga Aug 25 '17 at 18:14
  • 2
    @o11c I think you understand the most about the problem I'm having. Could you answer with code example? Thank you so much if you do. – Norak Aug 26 '17 at 08:31
  • @Norak The StackOverflow bureaocracy is being useless as usual, and requests that you formally comment "the marked duplicate has nothing to do with my question". I can't answer until the question is reopened. – o11c Aug 26 '17 at 19:43
  • 3
    @Norak Okay, I've answered the new dup. – o11c Aug 26 '17 at 19:57

0 Answers0