I kinda do it now because I took a job where I don't code anymore. I did enjoy doing it and getting paid for it though.I code all day long, can't even imagine coding outside of work for pleasure/practice
My python is minimal. Just to clarify you wish to create objects of type subclass without knowing what exactly subclass is, ex: abstract class Car could be subclasses to SportsCar, RaceCar, VintageCar, etc. Not knowing what it is I'll assume you mean what methods, variables, etc exist in said subclass. My next questions may be off but I'm just going to ask them from a Java perspective - would you be able to instantiate the subclass referencing the superclass constructor?Programming puzzle(i.e. do my homework for me).
I have an abstract base class and a third party is going to subclass it. How can I instantiate those subclasses, without knowing what they are explicitly, once and only once and store those objects for later use? This is in Python, so static is not fully available.
I would happily throw that benign baby out with the bath water if meant to I could be rid of such evils as SOME_REGISTER actually preprocessing to a multiline platform-dependent function call that your debugger can't resolve.
Or you use stuff like local, object, class, or even global(ugh) variables instead. Allowing preprocessing in your language is essentially kicking the can of worms down the road.
Objects in C????
#defines have been industry standards in my experience
Never heard of LeetCode, what do you use it for?
No idea. My C++ is non existent. Hoping to keep it that way.Maybe like in C++ where you can define a functions local variables as static to have them not be volatile. I'm not sure. My.python is low too
I am assuming Python 3, and I am not quite sure I understood the problem--but here we go:How can I instantiate those subclasses, without knowing what they are explicitly, once and only once and store those objects for later use? This is in Python, so static is not fully available.
class SuperClass:
instances = dict()
def __new__(self, *args, **kwargs):
return self.instances.setdefault(self, super(SuperClass, self).__new__(self, *args, **kwargs))
def __init__(self):
print("SuperClass::__init__() - I was initialiazed as", self.__class__.__name__)
def whatami(self):
""" return class name of object """
return self.__class__.__name__
class SubClass(SuperClass):
def __init__(self):
SuperClass.__init__(self)
if __name__ == "__main__":
tsuper = SuperClass()
print("tsuper is a", tsuper.whatami(), tsuper)
tsub1 = SubClass()
print("tsub1 is a", tsub1.whatami(), tsub1)
tsub2 = SubClass()
print("tsub2 is a", tsub2.whatami(), tsub2)
print(tsuper.instances)
SuperClass::__init__() - I was initialiazed as SuperClass
tsuper is a SuperClass <__main__.SuperClass object at 0x000002012CEE9278>
SuperClass::__init__() - I was initialiazed as SubClass
tsub1 is a SubClass <__main__.SubClass object at 0x000002012CEE93C8>
SuperClass::__init__() - I was initialiazed as SubClass
tsub2 is a SubClass <__main__.SubClass object at 0x000002012CEE93C8>
{<class '__main__.SuperClass'>: <__main__.SuperClass object at 0x000002012CEE9278>, <class '__main__.SubClass'>: <__main__.SubClass object at 0x000002012CEE93C8>}
if self not in self.instances:
self.instances[self] = super(SuperClass, self).__new__(self, *args, **kwargs)
return self.instances[self]
Lol. I did admire it on foh dark theme.Don’t you guys love the contextual coloring for code blocks?
Thanks Foler and Neranja , you were both helpful in piecing together the answer. Apparently, Python lets the super know of the existence of all subs, making this much easier. So, the collection of subs will be a class member on the super and any time someone requests said collection, the super will check a class flag and instantiate the subs if the flag is False.
I could probably just check if the collection is empty too, that's probably 99.9% correct too.
No, the homework comment was a joke, this is for work.Lol. I did admire it on foh dark theme.
I'm no python expert but I'll assume OO python follows traditional OO principles like inheritance and polymorphism.
Is this for a class?
Lol. Serious question, what's your beef with Java? Do you primarily program in C++ at the moment and if so in that context? I've mainly seen C++ used in gaming, fintech, and robotics iirc.Can we ban foler for that