how do I use a python module as a class?

This is the place for queries that don't fit in any of the other categories.

how do I use a python module as a class?

Postby Tcll » Thu Feb 06, 2014 4:24 pm

so here's what I want to do:
Code: Select all
import module
   
mvar = module()

I've seen it done with the PyQt modules,
but I don't feel like researching a complex code just to figure out a simple usage. :P

anyone know how this works??
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: how do I use a python module as a class?

Postby stranac » Thu Feb 06, 2014 4:29 pm

You really shouldn't be doing stuff like that...
Where did you see it?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

Re: how do I use a python module as a class?

Postby Tcll » Thu Feb 06, 2014 4:38 pm

PyQt...
some of the modules can be imported and used like that...

I wanted to do that for my complex modelling interface, instead of doing "from scene import scene"
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: how do I use a python module as a class?

Postby stranac » Thu Feb 06, 2014 4:51 pm

I would recommend not doing stuff like this, but you can, if you really have a good reason.
I'll just link you to this SO answer by Alex Martelli: http://stackoverflow.com/a/1060872
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

Re: how do I use a python module as a class?

Postby Tcll » Thu Feb 06, 2014 5:10 pm

ah, I can see why it's not recommended,
but that aside, it just replaces the module data with the class data,
so in retrospect, I assume it's almost exactly like "from module import class as module" (in a way)
which shouldn't pose any REAL performance bamf other than with initialization... heh
(shouldn't be anything big for the sake of simplicity)

the only thing I'm concerned about it global vs local...
but even then I can just do sys.modules[name] to access the global data there... heh

but hey if it fails, I can always resort to importing the module itself and using the global reference there... heh

thanks :)

EDIT:
a slightly better reference: http://www.daniweb.com/software-develop ... s-a-class#

thanks again ;)
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], stranac and 3 guests