-3

I am relatively new at python and am trying to accomplish the following:

class A:
       def __init__(self,name,L1):  
       self.name=name
       self.L1=[0,0]

class B:
    def __init__(self, person_names):
    #this is where person_names are entered in the program
    #person_names is used as object parameter while creating objects of class A

I want to create objects of class A within B using names as entered by the user. Then I want to append these objects to a list. Can someone please tell me how to accomplish this?

Janmajay
  • 53
  • 1
  • 6
  • What are name and L1? – Padraic Cunningham Mar 05 '16 at 01:00
  • 2
    I'm voting to close this question as off-topic because SO is neither a coding service nor a homework completion service. – TigerhawkT3 Mar 05 '16 at 01:01
  • @TigerhawkT3, how do you know it is homework? – Padraic Cunningham Mar 05 '16 at 01:02
  • @PadraicCunningham - I don't. That's why I also mentioned "coding service." However, homework is a distinct possibility. And, while homework questions are okay, "here's my assignment, now do it for me" isn't an appropriate "question" for SO. – TigerhawkT3 Mar 05 '16 at 01:04
  • @TigerhawkT3 I'm sorry but I'm trying to learn how to use objects of one class in another class. That is my basic question. – Janmajay Mar 05 '16 at 01:05
  • @Janmajay, how do the "list of objects" in your title fit into what you are trying to do? – Padraic Cunningham Mar 05 '16 at 01:06
  • @PadraicCunningham I have to make several objects each having different name parameters and then store them in a list to perform further operations later on. But I guess that is incorrect framing. I'll change it. Thanks. – Janmajay Mar 05 '16 at 01:09
  • Is person_names a list of names? You would be better adding the exact logic with some examples as your question is a bit unclear – Padraic Cunningham Mar 05 '16 at 01:10
  • @PadraicCunningham Yes,it is a list of all names. – Janmajay Mar 05 '16 at 01:11
  • Then just iterate over the list of names in a list comp and create instances with `A(name)` – Padraic Cunningham Mar 05 '16 at 01:12
  • @PadraicCunningham But then, how would class B access class A without an instance of class A being passed? I refer to the answer in this link: http://stackoverflow.com/questions/19993795/how-would-i-access-variables-from-one-class-to-another – Janmajay Mar 05 '16 at 01:15
  • No, you can access A in B the same way you can use a function in anther function once it is defined before you use it, `list(map(A, person_names))` would create a list of instances – Padraic Cunningham Mar 05 '16 at 01:17
  • @PadraicCunningham But in that case, what is the difference between the answer in the link and my question? (You have been really helpful but I absolutely need to get this straight). – Janmajay Mar 05 '16 at 01:19
  • There is no real difference, you will have the instances in your list so if you want to access their attributes you can simply inst.whatever, what are you actually trying to achieve? – Padraic Cunningham Mar 05 '16 at 01:24

1 Answers1

0

Not sure if I understand you but, assuming you have a list of names already collected from the user:

class B:
    def __init__(self, person_names):
        self.objs = []  # list to contain objs
        for name in person_names:
            self.objs.append(A(name))  # create class A object and add to list

OR

    class B:
        def __init__(self):
            self.objs = []  # list to contain objs
            while True:
                name = input('Enter a name: ')
                if not name: break   # empty string signals end of input
                self.objs.append(A(name))  # create class A object and add to list
SoreDakeNoKoto
  • 1,175
  • 1
  • 9
  • 16