[Tutor] Creating an Identifier or Object Name from a String?

Hazlett, Les les.hazlett at navteq.com
Sat Feb 10 19:58:19 CET 2007


Hi,

I read the information at http://mail.python.org/mailman/listinfo/tutor
but am still not sure how to properly send a question.  Will I get an
email response without joining the list?  

Thanks for any help.

Les

 

I thought when I read the 2002 thread with the subject (Creating an
Identifier or Object Name from a String?), that I had found a solution
to my problem.  But, I can't make the concept work for creating an
object identifier.  

THIS WORKS

	class nsf(object):
	    # name = ''
	    def __init__(self, id):
	        self.name = id 
	 
	# main
	Ireland = nsf('Ireland')

BUT - THIS DOESN'T

	class nsf(object):
	    # name = ''
	    def __init__(self, id):
	        self.name = id 
	 
	# main
	cmdstr = "Ireland = nsf('Ireland')"
	eval(cmdstr)

-------------------------

I get an error saying the following

      eval(cmdstr)
  File "(string)", line 1
      Ireland = nsf('Ireland')
              ^
SyntaxError: invalid syntax

-------------------------

For reference the thread message that I am referring to is:

Gregor Lingl wrote:

	I Tried it and it worked (?!)
	
	Python 2.2c1 (#27, Dec 14 2001, 13:15:16) [MSC 32 bit (Intel)]
on win32
	Type "copyright", "credits" or "license" for more information.
	IDLE 0.8 -- press F1 for help
	>>> class K:
	        pass
	
	>>> field1 = K()
	>>> field2 = K()
	>>> field3 = K()
	>>> for ct in range(1,4):
	        obj="field"+str(ct)
	        objTC=eval(obj)
	        objTC.text=['a','b',str(ct)]
	
	
	>>> field1.text
	['a', 'b', '1']
	>>> field2.text
	['a', 'b', '2']
	>>> field3.text
	['a', 'b', '3']
	>>>
	
	Gregor
	
	

What he did also works for me. Bur, I have tried many ways to use the
same idea for creating an object identifier.  I haven't found a way yet.

 

 

 


The information contained in this communication may be CONFIDENTIAL and is intended only for the use of the recipient(s) named above. If you are not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication, or any of its contents, is strictly prohibited. If you have received this communication in error, please notify the sender and delete/destroy the original message and any copy of it from your computer or paper files.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070210/9b685555/attachment.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 145 bytes
Desc: Blank Bkgrd.gif
Url : http://mail.python.org/pipermail/tutor/attachments/20070210/9b685555/attachment.gif 


More information about the Tutor mailing list