Search code examples
pythonthrift

How to display object properties in python


In my project, I have to user build server and client in different language by integrate thrift to the project. So I decided to build client in python. However I know very little about python :(

So can anyone help to solve this simple problem

Assume that I have a class with those properties:

struct Exam {
    1:i64 examId,
    2:string student,
    3:i64 graded
}

In my server size, I have implement that code that return a list. So how can I read the the name of student from each exam in my client side (python).

I have tried this:

mylist = readExam();
print(mylist)     # This display correctly

#Integrate the list and get the student name
for i in mylist
   print "student: " % mylist.student

Solution

  • Your problem is just what you are printing when iterating through mylist. For loops in python work similarly to foreach loops in java and other languages: the i takes on the value of each iterable in mylist in turn. mylist is just a list of exams (e.g. think array list).

    So you want:

    for i in mylist:
       print "student: %s" % i.student
    

    Or, in python 3 friendly code:

    for i in mylist:
       print("student: %s" % i.student)