PDA

View Full Version : Add functions to a class at runtime(python)



KingKong
01-11-2012, 03:37 AM
This is my folder structure:

PySRL/
pymml/
__init__.py
libsmart.dll
mtypes.py
SMART.py

Includes/
__init__.py

core/
__init__.py
gametab.py
login.py

SMART.py contains one class named Smart. I wish to add all the functions within gametab.py and login.py(more files to come later on) to the class Smart at runtime. Whats the best to do this dynamically?(As in i run another python file which iterates through all the modules inside core)

Dan Cardin
01-11-2012, 01:54 PM
class Smart()
__init__(self, Anyclass, anyclass2):
self.gametab = Anyclass
self.login = anyclass2

meow = Class(gametab, login)
like that? or am i misunderstanding

KingKong
01-11-2012, 11:02 PM
see how you wrote self.gametab? I want it to loop through all the files in includes and then add all the functions in all those files to class Smart at runtime, instead of hardcoding them.

Dan Cardin
01-12-2012, 01:56 PM
oh i thought you just wanted to import a single class's functions. I'm not sure you could do it without at least already having the names of all of the classes hardcoded somewhere in here.

slacky
02-27-2013, 08:10 PM
Edit: And then i noted that the thread is a hundred years old.