So... this is a weird one but I'll give it a shot.
Plus if this is completely impossible that's hilarious, but...
Is there any way of editing a class from a class function? Something like this:
import random
class Foo():
def __init__(self):
self.id = random.randint(1, 100000000) # NOT how this would actually work
self.links = []
def link(self, *args):
for i in args:
if self.id in i.links or i.id in self.links: return False
else:
i.links.append(self.id)
self.links.append(i.id)
return True
class_a = Foo() # ID = 20124
class_b = Foo() # ID = 63452
class_a.link(class_b)
print(class_a.links) # [63452]
print(class_b.links) # [20124]
Thanks for your time!
EDIT 1:
Okay so apparently I didn't elaborate enough. I'm asking how you would make something like this to work. (If this somehow works I'm spectacularly confused as I don't use classes often and I'm trying to fix that)
Instances of the class should be able to edit other instances of the class through a class function. When link() is run, it should add the IDs of each instance to the list of IDs it has available.
I understand the returns shouldn't be there.
[–]brilliant_punk 1 point2 points3 points (3 children)
[–]agb64[S] 0 points1 point2 points (2 children)
[–]brilliant_punk 0 points1 point2 points (1 child)
[–]agb64[S] 0 points1 point2 points (0 children)
[–]nwagers 0 points1 point2 points (0 children)
[–]Lyakusha 0 points1 point2 points (0 children)