I want to create a set of classes, each with their own unique name. Something like this:
class B(object):
# existing_ids = []
existing_ids = set()
@staticmethod
def create(my_id):
if my_id not in B.existing_ids:
# B.existing_ids.append(my_id)
B.existing_ids.add(my_id)
# return B(my_id)
else:
return None
# Added block
if style == 'Ba':
return Ba(my_id, style)
else:
return None
def __init__(self, my_id):
self.my_id = my_id
self.style = style # Added
# Added function
def save(self):
with open('{}.pkl'.format(self.my_id), 'ab') as f:
pickle.dump(self.data, f, pickle.HIGHEST_PROTOCOL)
# Added function
def foo(self):
self.data = 'B_data'
# Added class
class Ba(B):
def __init__(self, my_id, style):
super().__init__(my_id, style)
def foo(self):
self.data = 'Ba_data'
# Edited part
a = B.create('a', 'Ba')
b = B.create('b', 'Ba')
c = B.create('b', 'Ba')
print(B.existing_ids, a.existing_ids, b.existing_ids, c)
# {'a', 'b'} {'a', 'b'} {'a', 'b'} None
Is this a good idea? Are there better or other ways to do this?
EDIT: I understand that my example was a bit confusing. I've now updated it a bit to better show what I am trying to achieve. For my problem I will also have class Bb(B), class Bc(B), etc.
This thread seems like the most related:
Static class variables in Python
The basics:
Python - Classes and OOP Basics
Metaclasses could be relevant, but it also goes a bit over my head:
What is a metaclass in Python?
Classmethod vs static method:
Meaning of @classmethod and @staticmethod for beginner?
What is the difference between @staticmethod and @classmethod in Python?