473,395 Members | 1,386 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,395 software developers and data experts.

classobj?

Hi,

I am trying to create classes at runtime based on input from a textfile.
I am trying to use the function new.classobj. I am able to create the
new classes successfully, however I fail to understand on how to add
this new class to the current dictionary.

cl = new.classobj('SomeClass', (BaseClass, ), {})

After this call, how do I instantiate SomeClass?

I understand cl() will instantiate this, however this defeats my
purpose, since the name of the class is obtained at runtime.

Thanks

venky
Feb 27 '07 #1
3 2419
On Feb 26, 5:43 pm, Venky <v...@nospam.comwrote:
Hi,

I am trying to create classes at runtime based on input from a textfile.
I am trying to use the function new.classobj. I am able to create the
new classes successfully, however I fail to understand on how to add
this new class to the current dictionary.

cl = new.classobj('SomeClass', (BaseClass, ), {})

After this call, how do I instantiate SomeClass?

I understand cl() will instantiate this, however this defeats my
purpose, since the name of the class is obtained at runtime.

Do you mean that you want to add it to globals()?

globals()['SomeClass'] = cl

myinst = SomeClass()
print isinstance(myinst, SomeClass)
print isinstance(myinst, BaseClass)

--
Hope this helps,
Steven
Feb 27 '07 #2
....
>
Do you mean that you want to add it to globals()?

globals()['SomeClass'] = cl

myinst = SomeClass()
print isinstance(myinst, SomeClass)
print isinstance(myinst, BaseClass)

--
Hope this helps,
Steven
Thanks. That's what I was looking for.
Feb 27 '07 #3
In <Xn****************************@216.77.188.18>, Venky wrote:
>Do you mean that you want to add it to globals()?

globals()['SomeClass'] = cl

myinst = SomeClass()
print isinstance(myinst, SomeClass)
print isinstance(myinst, BaseClass)

Thanks. That's what I was looking for.
Probably not because now you have to know the name when you write the
static source code. You said the names are discovered at runtime, so how
do you know it's `SomeClass`? Put you classes into a dictionary with the
name as key.

`globals()` is a dictionary too, but it is more work to get an object by
name from it (globals()['SomeClass'] vs. some_dict['SomeClass']) and you
don't risk that some of your dynamically created classes overwrites an
existing one with the same name.

Ciao,
Marc 'BlackJack' Rintsch

Feb 27 '07 #4

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...
12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
1
by: Brad Clements | last post by:
I need to dynamically create a new type of class at runtime, based on an oldstyle class and a new style class. I'm using Python 2.3.2 My code: def shipment_from_db_instance(db_shipment):...
4
by: venk | last post by:
Hi, given below is my interaction with the interpreter.... In one case, i have created the class method using the "famous idiom"... and in the other, i have tried to create it outside the class...
7
by: Wim Roffil | last post by:
I have a simple webpage (without stylesheet) where I want to be able to switch some parts of the page off. I tried to do this by giving all the elements that should be switched off the class...
2
by: Létező | last post by:
I use Python 2.4.4. Please read the code below: ----------------------------------------------------------- from new import classobj def mymeta(name,bases,clsdict): print 'meta: %s'%name...
3
by: Wesley Brooks | last post by:
Dear Users, I'm in the process of adding assert statements to a large piece of code to aid with bug hunting and came across the following issue; Using python in a terminal window you can do...
3
by: none | last post by:
I have a very complex data structure which is basically a class object containing (sometimes many) other class objects, function references, ints, floats, etc. The man for the copy module states...
3
by: westymatt | last post by:
I have a class where a parameter to its constructor is a type(param) = 'classobj'. How would I go about instantiating that given it has no constructor.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.