Fabian
Fabian

Reputation: 129

How to create "individual" objects

my task is actually pretty simple. Maybe I'm too much used to c++ to not see my fault. I have two classes and a list. The list should include all objects made of class1, while class1 also includes a list for all objects of class2. So we have:

All_Objects1 = []     # For class1 objects

class class1 (object):
    All_Objects2 = []  # For class2 objects

class class2 (object):
    name = ""
    number = 0

Now I do this:

# class1 objects:
obj1= class1() 
obj2= class1()

# class2 objects
obj3 = class2()
obj3.name, obj3.number = "hello world", 10
obj4 = class2()
obj4.name, obj3.number = "hello europe", 20
obj5 = class2()
obj5.name, obj3.number = "hello asia", 30
obj6 = class2()
obj6.name, obj3.number = "hello africa", 40

# Attach object3 and object4 to object1
obj1.All_Objects2.append(obj3)
obj1.All_Objects2.append(obj4)

# Attach object5 and object6 to object2
obj2.All_Objects2.append(obj5)
obj2.All_Objects2.append(obj6)

# Attach obj1 and obj2 to the global list.    
All_Objects1.append(obj1)
All_Objects1.append(obj2) 

And finally I do a print for checking if everything is where it belongs:

print (len(All_Objects1[0].All_Objects2)) # Should be 2.
for i in All_Objects1[0].All_Objects2: 
    print (i.name)                        # Should be world and europe.

print (len(All_Objects1[1].All_Objects2))     # Should be 2.
for i in All_Objects1[1].All_Objects2:        # Should be asia, africa.
    print (i.name)

Now the problem is, that every object2 is in every object1 and I have no clue why.

In my actual program I do something like this, what would actually work in c++:

#pseudo
for i in all_information:
    if (i==Keyword):
        currentObject = class1()
        All_objects.append(currentObject)

And then I will have some independent objects in my list. But in Python I think I somehow can't overwrite "currentObject".

Thank you so much for your help.

Upvotes: 1

Views: 147

Answers (3)

Marco smdm
Marco smdm

Reputation: 1045

You need just to initialized your class. init and you need only one All objects list. An example with class you can find below.

class firstA():
    def __init__(self):
        self.All_Objects = []

class secondB():
    def __init__(self):
        self.name ="None"
        self.number = 0

When you are calling the class the initialization will be done :-) That is very nice in python2/3.

Whatever you initialized outside the class will be considered as a static variable! So in your example the All_Objects1 list for instance.

Hope this clarify your doubts! Have a nice day.

Upvotes: 1

Tryph
Tryph

Reputation: 6209

With Python, all class members declared outside any function are static members. So they are shared by each object instantiated from the class.

What you want is the following:

class class1 (object):
    def __init__(self):
        self.All_Objects2 = []  # For class2 objects

class class2 (object):
  def __init__(self):
        self.name = ""
        self.number = 0

Upvotes: 1

Rafael Aguilar
Rafael Aguilar

Reputation: 3279

In Python somethings could be making this error, first which version×of Python are you using?

I have not tested on my console yet but I would guess that it's a initialization issue, for how you are declaring the list, I would rather suggest to use a more Pythonic approach, I mean, build a constructor and use keyword self for instance variables, if this works, I can extend my answer explaining why this happened.

Create a method like this in class 1 and class 2:

def __init__(self, object_list=None) : # use corresponding classes attributes.  
    if self.name_of_your_variable:
           self.name_of_your_variable= [] # or proper value

Adapt this to your classes and let me know, if it works, as soon I get home I'll extend it

Upvotes: 2

Related Questions