0

In Python 2.7, I'm trying to create classes within classes (and so on), like for example:

class Test(object):

    def __init__(self, device_name):
        self.device_name = device_name

    class Profile(object):
        def __init__(self, x1, x2):
            self.x1 = x1
            self.x2 = x2

    class Measurement(object):
        def __init__(self, x1, x2):
            self.x1 = x1
            self.x2 = x2    

Using this layering to create objects, I need to be able to assign and access any variable in any layer, like for example:

test1 = Test("Device_1")
Test.Profile(test1, 10, 20)
Test.Measurement(test1, 5, 6)

print test1.Profile.x1
print test1.Measurement.x1 

It should also be noted that I need to load the classes with data taken from a text file.

I thought that using classes would be the best way of achieving this but I'd be happy to hear any other ideas.

4
  • 1
    why you create classes within classes? Commented Nov 29, 2016 at 13:33
  • What's your question? Commented Nov 29, 2016 at 13:36
  • You can put Profile and Measurement classes to separate file and return just instances of these classes from Test class. You don't need to do that nesting. Commented Nov 29, 2016 at 13:42
  • Possible duplicate of python nested classes Commented Nov 29, 2016 at 13:45

2 Answers 2

0

My version/solution class scopes:

class Test(object):

    def __init__(self, device_name):
        self.device_name = device_name

    class Profile(object):
        def __init__(self, x1, x2):
            self.x1 = x1
            self.x2 = x2

    class Measurement(object):
        def __init__(self, x1, x2):
            self.x1 = x1
            self.x2 = x2

test1 = Test("Device_1")
prof = test1.Profile(10, 20)
meas= test1.Measurement(5, 6)

print (prof.x1)
print (meas.x1) 

>>> 10
>>> 5
Sign up to request clarification or add additional context in comments.

Comments

0

Though I don't know why you want nested classes, this will do exactly as you want. If you look at the example, be sure to note the change in syntax.

class Test(object):
    class Profile(object):
        def __init__(self, x1, x2):
            self.x1 = x1
            self.x2 = x2

    class Measurement(object):
        def __init__(self, x1, x2):
            self.x1 = x1
            self.x2 = x2

    def __init__(self, device_name):
        self.device_name = device_name
        self.profile = None
        self.measurement = None

    def make_profile(self, a, b):
        self.profile = self.Profile(a, b)

    def make_measurement(self, a, b):
        self.measurement = self.Measurement(a, b)

test1 = Test("Device_1")
test1.make_profile(10, 20)
test1.make_measurement(5, 6)

print (test1.profile.x1)
print (test1.measurement.x1)

Output:

10
5

7 Comments

are the extra methods really needed?
In your solution, you create separate objects, whereas mine has them all in the same object. You could theoretically pass in all the arguments in the original constructor, but it didn't seem like that's what he wanted.
in your case, you can't access the profile and measurement for test1 from test1 because you are creating them separately. I don't agree with the whole nesting classes thing, but the OP seemed to want to be able to access profile and measurement from within the Test object, which i have provided functionality for.
That's what I was aiming for. Would it be possible to add another class within 'Profile' or 'Measurement'?
Yes, but there are better ways to go about doing what it seems you want
|

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.