1

Can you suggest clean way to represent this struct in python?

students = struct;
students(1).fname = 'john';
students(1).lname ='smith';
students(1).height = 180;

students(2).fname = 'dave';
students(2).lname = 'clinton';
students(2).height = 184;

Thanks!

3
  • list of dictionaries or a list of a student class Commented Mar 28, 2018 at 11:41
  • can you please show an example? Commented Mar 28, 2018 at 11:42
  • I just wrote a class (named "tree") that makes possible to use the matlab-like syntax in python3. For the initial example, we could write something like: students = tree() students.john.lname ='smith' students.john.height = 180 students.dave.lname = 'clinton' students.dave.height = 184 students.other.lname = 'roger' students.other.height = 185 Let me know if someone if interested in having the source code of the "tree" class. Commented Jan 8, 2019 at 9:02

3 Answers 3

2

In Python, you can make a class (which is a bit like a struct but with more functionality) as follows:

class Student()
    def __init__(self, fname, lname, height):
        self.fname = fname
        self.lname = lname
        self.height = height

The __init__ function describes the information required to make a new student record. The 'self' argument just tells Python that you want to set these variables on an instance (an actual student record) rather than on the class (Which is a blueprint for student records). You can then create an instance of a Student as follows:

my_student = Student('Space', 'Man', 180)

You could make many of these instances and append each to a list:

all_students = [] # An empty list
all_students.append(my_student) # using the instance we've already created
all_students.append(Student('Jingle', 'Sting', 180)) # a new instance

You can then access the items in the list as follows:

all_students[1]

By creating a class, you can also create methods which apply to all instances of Students.

class Student()
    def __init__(self, fname, lname, height):
        self.fname = fname
        self.lname = lname
        self.height = height

    def get_height_in_meters(self):
        return self.height / 100

And use them like this:

my_student.get_height_in_meters()

You could even string accessing student records from the list directly with methods that apply to them like so:

all_students[1].get_height_in_meters()
Sign up to request clarification or add additional context in comments.

Comments

1

This can accomplished by creating a list of dictionaries as follows:

students = [
    {'fname': 'john', 'lname': 'smith', 'height': 180},
    {'fname': 'dave', 'lname': 'clinton', 'height': 184}
]

Then, you can get the n-th student by doing students[n] or a specific field from the n-th student by doing students[n]['fname']

Comments

1

Use a list of namedtuple(s):

import collections
harry = student(fname="harry", lname="Potter", height=160)
hermione = student(fname="hermione", lname="Granger", height=140)

students = [
    harry,
    hermione
]
print(students[0].fname)
print(students[1].fname)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.