XandeGil
XandeGil

Reputation: 13

Struct/Field Variables with Python

I would like to know if it is possible to create struct variables and their respective fields in Python as it is possible in MATLAB.

For example: I want to create a struct variable with the name of a person and their characteristics as fields:

John.name = 'John'
John.age = 30
John.sex = 'Male'
John.height = 1.85
John.weight = 85

Then I will perform some operations with these variables, for example checking if John is over 18:

if John.age > 18: ....

Upvotes: 1

Views: 2300

Answers (4)

abdullah
abdullah

Reputation: 61

you can use dictionaries for example

John = {
'name'   :  'John'
'age'    :  30
'sex'    :  'Male'
'height' :  1.85
'weight' :  85
}

if John['age'] > 18:
    print('older than 18')

or other way you can use OOP "Objective oriented programming" for example:

    #creation class with name person:
    class person:

        #define init function to initialize main variables:
        def __init__(self, name, age,sex,height,weight):

            #define main variables you want to store and use
            self.name    = name
            self.age     = age
            self.sex     = sex
            self.height  = height
            self.weight  = weight
    
    #using class to store data you  want:
    John = person('John',30,'Male',1.85,85)
    Mario = person('Mario',25,'Male',1.90,80)

    #using data you stored:
    print(John.name)
    print(John.age)
    print(Mario.sex)
    
    #and your example is ready
    if John.age > 18: ....

Upvotes: 0

Ondrej K.
Ondrej K.

Reputation: 9664

There are different constructs that you can use and which one is the most suitable depends on what exactly you intend to do with it. With the example you've given and assuming the values of the attributes are not changing, namedtuple could be a good candidate here:

from collections import namedtuple

Person = namedtuple("Person", ["name", "age", "sex", "height", "weight"])

John = Person("John", 30, "Male", 1.85, 85)

if John.age > 18:
    ...

Upvotes: 1

William Torkington
William Torkington

Reputation: 407

Structs are not present in python, as everything is looked at as an object.

My best suggestion is to make a class for what you originally intended the structure to do. Something similar to:

class Person:
  def __init__(self, name, age, height, weight):
    self.name = name
    self.age = age
    self.height = height
    self.weight = weight

# creating the object
john = Person('John', 20, 184, 75)

# Accessing the fields

if john.age > 18:
  ...

Alternatively, you could use a dictionary


John = {}

# setting a field
John['age'] = 20

# using a field
if John['age'] > 18:
  ...

Upvotes: 1

AlanTheKnight
AlanTheKnight

Reputation: 96

You can also use classes to implement struct.

class Person:
    def __init__(self, **kwargs):
        for i in kwargs:
            setattr(self, i, kwargs[i])

john = Person(name="John", age=30)
print(john.name, john.age)  # John 30

Upvotes: 0

Related Questions