Brad D
Brad D

Reputation: 67

Classes with multiple attribute levels [Beginner]

Does python have a similar functionality with its data structure similar to what can be seen in MATLAB?

For example, being able to call a class similar to what follows:

variable = Class(inputs)

would allow to call variable with multiple attributes (not sure if attributes is the correct terminology here).

variable.attribute1.attribute2

In other words, defining a Class would provide the functionality of these multiple attributes. I am only seeking documentation if such functionality exists.

I'll add a little example because my question is quite vague. So, I'll define a class that assigns data sets to a variable.

class Properties(object):

    def __init__(self, data_set1, data_set2):
        self.Data1 = data_set1
        self.Data2 = data_set2

Now, I'll define a variable.

variable = Properties([1, 2, 3, 7], [3, 9, 2, 8])

Obviously now I can call variable with its individual data sets as:

variable.Data1
variable.Data2

What I desire is to have another Class that will be able to then define Statistical properties of the sets, such as:

variable.Data1.Maximum
variable.Data1.Minimum
variable.Data2.StandardDev

Upvotes: 4

Views: 4110

Answers (2)

chepner
chepner

Reputation: 532518

class Attribute(object):
    pass

class MyClass(object):
    def __init__(self):
        self.attribute1 = Attribute()

variable = MyClass()
variable.attribute1.attribute2 = 5

Upvotes: 6

Paul Becotte
Paul Becotte

Reputation: 9997

A class is an object that has attributes (python objects... in python, everything is an object) and behavior (functions).

When you initialize a class with Class(inputs) it actually runs that classes constructor, which can set up some or all of the resulting objects attributes, such as variable.attribute1

An attribute can be any data type... numbers, strings, or even other classes. This means that attribute1 could be an object of Class2, which has it's OWN attribute named attribute2 (which could be an object of any type, including another class, even an object of type Class or Class2 if you wanted to get crazy lol).

So, the TL;DR ... yes.

Upvotes: 0

Related Questions