473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how do I make a class global?

Hi,

I thought it would be nifty to create a class that created other classes for
me. The method below shows what I would like to do. The problem is that the
class the method creates is local to the method. Is it possible to make the
class visible in the global scope so I can import the module see the
dynamically created classes? Or do I need to generate a source file and do a
'from tmp import *'?

def new(self, eventType, param):
self.value += 1
exec 'global %s; %s = %d' % (eventType, eventType, self.value)
sl = []
sl.append('class %sEvent(QEvent):' % eventType)
sl.append(' def __init__(self, %s):' % param)
sl.append(' QEvent.__init__(self, %s)' % evenType)
sl.append(' self.%s = %s' % (param, param))
source = '\n'.join(sl)
co = compile(source, 'tmp.py', 'exec')
exec co

Then, to create another event, I would just have to add another line like
this:
e.new('ETestEvent', 'test')

Thanks,
Tom
Apr 27 '06 #1
1 2376
basically, you can create new types on the fly using type() with three
arguments:

my_class = type("className",(BaseClass,),class_dict)

then, you can assign this vlass to the golbal namespace using
globals():

globals()["className"] = my_class

In your case, you would need to populate the class_dict by a function
object
that you parameterize to your needs, e.g.

def create_class(name,params) :
def cls_init(self) :
BaseClass.__init__(self)
self.params = params

cls_dict = {
"__init__" : cls_init
}

new_cls = type(name,(BaseClass,),cls_dict)
globals()[name] = new_cls

the result would be like this:
create_class("test_class",42)
instance = test_class()
print instance.params

42

Apr 27 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
1
by: Fernando Arámburu | last post by:
Hy everybody, I´m working on an ASP.NET framework and and I need to extend System.Web.HttpApplication. I mean, I need to put some intermediate class between System.Web.HttpApplication and Global...
2
by: Shapper | last post by:
Hello, In the main root of my web site together with my aspx and aspx.vb files I have the file global.vb. This file has a class with all the functions which are used in many aspx.vb files and...
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
3
by: Jeff | last post by:
Quick questions I can't find answers to: Is there a shortcut to do this: if(! $some_var){$some_var = 'some_other_value'} I'm used to this: $some_var ||= 'some_other_var'; PHP manual doesn't...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.