0

I am trying to get a list of member variables of a class.

class Book(object):
    def __init__(self):
        self.title='Inferno'
        self.author = 'Dan Brown'
        self.publisher= 'DoubleDay'
        self.pages=480

bk = Book()
p=bk.__dict__
print p.keys()

The output is:

['publisher', 'author', 'pages', 'title']

I am curious here as the list is neither printedalphabetically nor according to the way I listed the class variables. So in what way does python print it out?

bachkoi32
  • 1,426
  • 4
  • 20
  • 31
  • FTR in Python 3 you can do this: http://docs.python.org/3/reference/datamodel.html#metaclass-example – jamylak May 29 '13 at 10:54

2 Answers2

2

Its completely arbitrary, because it is a dictionary, and a dict is unordered.

(Well, not arbitrary, but more-or-less random, according to the way the computer stores the data).

James
  • 2,635
  • 5
  • 23
  • 30
1

Python stores class variables in a dict. This is an unordered data structure, so Python is free to choose whatever order it likes.

Katriel
  • 120,462
  • 19
  • 136
  • 170