IT/Software career thread: Invert binary trees for dollars.

Deathwing

<Bronze Donator>
16,403
7,399
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.
 
Last edited:

The_Black_Log Foler

Stock Pals Senior Vice President
<Gold Donor>
43,846
40,803
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.
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?

If I understand this correctly storing it isn't an issue, just use any ABT. Also not sure how static would relate at all to this question even in another OO language, irregardless like I said, minimal python so I may be way off
 

Noodleface

A Mod Real Quick
37,961
14,508
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
 

The_Black_Log Foler

Stock Pals Senior Vice President
<Gold Donor>
43,846
40,803
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

Lol. This back and forth. Sounds like deathwing is coming from the perspective of higher level programming and noodle lower level. There nuanced advantages to using preprocessor on an embedded level when programming in C depending on the mcu and compiler. For example x32 uses Uint_8t preprocessor to declare 8 bit ints. For example if you had a UART packet that required 8 bytes, for xc32 you would do something akin to Uint_8t RXBuff[packetsize] where packetsize would be 8. You wouldn't want to redefine your 8 bit int everywhere and in C the majority of compilers would interpret a primitive int as being either 16 or 32 bits, also depending on OS.

Tldr: neither one of you are wrong and deathwing brings up some valid and correct points in higher level programming. Just think you're not on the same page.


Never heard of LeetCode, what do you use it for?

Used for programming challenge practices. Same thing as hackerrank. Majority of big tech companies like Amazon, Facebook, Google etc pretty much use hackerrank problems to weed out first round applicants and it's becoming more popular. Your recruiter or application system just sends a potential applicant a programming problem that's timed. Mainly used to test knowledge of CS fundamentals (data structures/algorithms) as well as your ability to implement them in the language you choose.

It's kinda retarded but I guess it makes sense when you have a million kids who paid 40k to go to 12 week coding boot camps saturating your application pool.
 
Last edited:

The_Black_Log Foler

Stock Pals Senior Vice President
<Gold Donor>
43,846
40,803
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
No idea. My C++ is non existent. Hoping to keep it that way.
 

Neranja

<Bronze Donator>
2,605
4,143
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 am assuming Python 3, and I am not quite sure I understood the problem--but here we go:

If you declare a variable in Python inside the class definition (but outside the methods) the variable is static. This is also the reason you see a lot of "self.var = x" in __init__() functions.
So it's possible to get the __new__ operator to return already existing objects from the instances dict, or create a new one to both store in the dict and return.
Python:
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)

Output should be something like this:
Code:
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>}

Note: dict.setdefault() is a short way to write
Python:
if self not in self.instances:
    self.instances[self] = super(SuperClass, self).__new__(self, *args, **kwargs)
return self.instances[self]

CAVEAT: The code just short-circuited object creation at the __new__ level (like a singleton), but did NOT short-circuit the __init__() calls, so these calls WILL be called again and again when the object is reused. Depending on your use case this may be even what you want/need.

EDIT: LOL, the tsub2 printed the tsub1.whatami(). Not that it made a difference.
 
Last edited:
  • 1Like
Reactions: 1 user

Deathwing

<Bronze Donator>
16,403
7,399
Thanks Foler Foler and Neranja 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.
 

The_Black_Log Foler

Stock Pals Senior Vice President
<Gold Donor>
43,846
40,803
Don’t you guys love the contextual coloring for code blocks?
Lol. I did admire it on foh dark theme.
Thanks Foler Foler and Neranja 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.
👍

I'm no python expert but I'll assume OO python follows traditional OO principles like inheritance and polymorphism.

Is this for a class?
 

Deathwing

<Bronze Donator>
16,403
7,399
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?
No, the homework comment was a joke, this is for work.

The common use-case scenario is you sell a software product and your customers want to write plugins that interface with your API. In my case, the "software product" is our in-house test system and the "customers" are developers. I want to make this as fool proof as possible, which is why I don't want to depend on them instantiating the subclasses. The developers will write small subclasses that will efficiently scan gigantic logs and pull out pieces of information that they're interested in.
 
  • 1Like
Reactions: 1 user

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,964
So are you defining your superclass methods? If so then the use of "abstract base class" confused the fuck out of me.
 

The_Black_Log Foler

Stock Pals Senior Vice President
<Gold Donor>
43,846
40,803
Can we ban foler for that
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.

I love learning new languages and frameworks, at the moment diving deeper into Java web frameworks JavaScript frameworks and soon python. From my experience on my current job hunt it seems like these kinda things are most marketable.

Also can we get a sound off from other software engineers in here, what industry are you in and what languages/frameworks/tools do you primarily use?
 

Noodleface

A Mod Real Quick
37,961
14,508
My last program on defense was writing a toolset that used a Java front end with a c++ (c with classes) backend. Its the first and only time I've written Java and while I see the appeal, it just never spoke to me. High level languages are just not in my wheelhouse, and also the gui stuff was frustrating.

Im a C/C++ expert at this point so I just prefer it. I've done uefi/bios/drivers and now radar control in it. Just real low level stuff
 
  • 1Like
Reactions: 1 user