I have several classes’ instances. They are all using the same class. They all have a string parameter “id” and string parameter “joint”. The “joint” parameter’s value matches a string value of “id” parameter in one of other classes. I need to know how to obtain one parameter from a class which’s “id” parameter’s value is equal to current classe’s “joint” parameter.
Thanks!
Do you mean this?
class one:
def __init__(self, id, joint):
self.id = id
self.joint = joint
class two:
def __init__(self, id, joint):
self.id = id
self.joint = joint
obj1 = one(1, 2)
obj2 = two(1, 2)
if obj1.id == obj2.id and obj1.joint == obj2.joint:
print(True)
else:
print(False)
EDIT:
I also noticed that “id” is reserved(or at least Atom is telling me that) and you better replace it with something else.
No. I have 1 class, but multiple instances of that class. All of the class instances are made in a loop and stored in list.
I see an option to do a loop in class, though.
for class in classlist:
if class.id = self.joint:
self.joint_class = class
Must try out.
@adriansnetlis
you either inherit the ‘id’ as an instance attribute or as class attribute that is technically shared among all instances
class ONE:
myClsID = 1 # class attribute
def __init__(self):
self.myID = 2 # instance attribute
class TWO(ONE):# inherit from class ONE
def __init__(self):
super().__init__()# run class ONE init
print(self.__class__.myClsID, self.myID)
The class sounds like this:
class MyClass():
def __init__(self, id):
self.id = id
self.joint = joint
Sounds like an external assign of the values.
a = MyClass("a")
b = MyClass("b")
a.joint = "b"
b.joint = "a"
Is it that way? What happens when there is no “b” object?
Wouldn’t it be easier to refer to the object directly?
a.joint = b
b.joint = a
This way the referred object is present (or None).
You can’t obtain a parameter from a class as a class is a description of an object, not the object it describes.
It sounds you need either a registry of your objects, or a way to parse through your objects.
As you use strings to define your references I suggest to create a registry:
objectsById = {}
def register(object):
objectsById[object.id] = object
def get(id):
return objectsById[id]
...
def demonstrateCreation():
register(MyClass("a"))
register(MyClass("b"))
def demonstrateSetup():
a = get("a")
b = get("b")
a.joint = "b"
b.joint = "a"
def demonstrateUsage():
aJoint = get(a.joint) # as joint contains an id you need to ask the registry to get the object
The advantage of a registry is that you can easily access all objects, regardless if they have a connection or not.
Registry with direct links:
objectsById = {}
def register(object):
objectsById[object.id] = object
def get(id):
return objectsById[id]
...
def demonstrateCreation():
register(MyClass("a"))
register(MyClass("b"))
def demonstrateSetup():
a = get("a")
b = get("b")
a.joint = b
b.joint = a
def demonstrateUsage():
aJoint = a.joint # b is available without checking the registry.
You see the implementation of the registry is exactly the same.
But you can directly access the joint without looking at the registry.
I hope it helps somehow
Hi! No, I assign both objects(id and joint) on init of class. It’s when I create those classes in loop(looping through dictionary of data for them to use). I fixed this issue, however, by doing in next frame after class creation a for loop where I loop through a list of those classes and check if there is a class with id equal to current classe’s joint.
I suggest you follow monster’s advice. Create your classes first, then assign joints
id_to_instance = {}
id_to_join_id = {}
for some_id in some_id_sequence:
instance = SomeClass(some_id)
id_to_instance[some_id] = instance
for instance_id, instance in id_to_instance.items():
joint_id = id_to_join_id[instance_id]
joint = id_to_instance[joint_id]
instance.joint = joint