1

I am going to attach two blocks of code, the first is the main code that is ran the second is the testClass file containing a sample class for testing purposes. To understand what's going on it's probably easiest to run the code on your own. When I call sC.cls.print2() it says that the self parameter is unfulfilled. Normally when working with classes, self (in this case) would be sC.cls and you wouldn't have to pass it as a parameter. Any advice is greatly appreciated on why this is occuring, I think it's something to do with exec's scope but even if I run this function in exec it gives the same error and I can't figure out a way around it. If you'd like any more info please just ask!

import testClass

def main():
    inst = testClass.myClass()
    classInfo = str(type(inst)).split()[1].split("'")[1].split('.')
    print(classInfo)
    class StoreClass:
        def __init__(self):
            pass
    exec('from {} import {}'.format(classInfo[0], classInfo[1]))
    sC = StoreClass()
    exec('sC.cls = {}'.format(classInfo[1]))
    print(sC.cls)
    sC.cls.print2()
if __name__ == '__main__':
    main()
class myClass:
    def printSomething(self):
        print('hello')

    def print2(self):
        print('hi')
mizuprogrammer
  • 225
  • 3
  • 10
  • 1
    Don't use `exec`. Whatever you are trying to do, attempting to parse the output of `type(inst)` is almost certainly not the way to do it. – chepner Feb 03 '20 at 19:55
  • @chepner I've tried doing what I need many other ways with no luck, I'm not using exec out of choice more of need, I need to take an instance of a class and save it but since you can only save the variables of a class not the functions (to a text file) I need to store the string address to the class then recreate it is what I'm trying to do – mizuprogrammer Feb 03 '20 at 19:57
  • 2
    `inst.__class__` is the immediate type of your instance. Once you have that, `sC.cls` would not be an *instance* of the class, it would be the class itself, so you aren't supplying the instance that `print2` expects as its first argument. – chepner Feb 03 '20 at 20:00
  • @chepner so is there a way to fix this or a better way of doing this? This needs to be storable in a text file so the best way I've come up with is this way, storing a string of what file it's in and what the class name is then using exec, I'm aware it won't recreate the exact same instance but it will be an instance of the same class that I can then manually set all the variables to be the same as the old instance so it won't be the same instance persay but will have all of the same variables and functions – mizuprogrammer Feb 03 '20 at 20:03
  • Store *what* in a text file? If you want to serialize an object, that's what the `pickle` library is for. – chepner Feb 03 '20 at 20:06
  • @chepner I apologize I misread your second comment, I made a stupid mistake thinking sC.cls was an instance not the class sorry, now when I declare an instance of that it works exactly as I thought it should, thank you so much for your help! – mizuprogrammer Feb 03 '20 at 20:09

0 Answers0