2

I have a problem related to accessing objects created using one class from an another class

In order simplify the problem lets say ,

I Have a Python Files called Box1.py , cart1.py and soldApples.py

 class Box1:
    def__init__(self):
      self.appleList = []

    def set_Apples(self,number1):
      self.appleList.append(number1):

    def get_Apples(self):
        return appleList

And the Main Class Called Cart1.py

  from Box1 import Box1
  NewSet = Box1()

  class cart1:
     def calculation()
        numberofApples = 5
        NewSet.set_Apples(numberofApples)

      return 0

Now I want to Access Object "NewSet" using another class

let's say soldApples.py

so How do I access to "NewSet" Object Created by Cart1.py using soldApples.py?

Thank you very much in advance guys.

1
  • import Cart1 Cart1.NewSet Commented Mar 16, 2016 at 11:55

1 Answer 1

1

At the top of your soldApples.py file write the line:

import Cart1

and then in your file you can refer to the NewSet variable as:

Cart1.NewSet

Alternatively if you just want that variable you can do:

from Cart1 import NewSet

and then your variable will be called

NewSet

Also you will want the files to be in the same folder.

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

1 Comment

Hi Menyah Thank you very much for the quick response. made my day

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.