473,394 Members | 2,090 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,394 software developers and data experts.

Converting string to argument



class Foo:
x = "blah"
y = "blah some more"

foo = Foo()
x_string = "x"
y_string = "y"

# I want something like this to work:
print foo.x_string
print foo.y_string
# The above should print out "blah" and "blah some more"
# Any ideas?
# Let me know if I need to clarify anything.


Joe Laughlin
NCO Simulation Labs
Integrated Defense Systems
The Boeing Company


Jul 18 '05 #1
5 1539
On Thu, 1 Jul 2004 18:07:16 -0700,
"Laughlin, Joseph V" <Jo***************@boeing.com> wrote:
class Foo:
x = "blah"
y = "blah some more" foo = Foo()
x_string = "x"
y_string = "y" # I want something like this to work:
print foo.x_string
print foo.y_string
# The above should print out "blah" and "blah some more"
# Any ideas?
# Let me know if I need to clarify anything.


The usual comp.lang.python answer is "why do you want to do this?"

In most cases, wanting to do this is a symptom of something else
being sub-optimal. Perhaps it's as simple as putting x and y into
a dictionary instead of creating them as class attributes:

foo = { }
foo[ "x" ] = "blah"
foo[ "y" ] = "blah some more"

x_string = "x"
y_string = "y"

print foo[ x_string ]
print foo[ y_string ]

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #2
On 02 Jul 2004 08:08:59 -0400, Heather Coppersmith <me@privacy.net> wrote:
The usual comp.lang.python answer is "why do you want to do this?"

In most cases, wanting to do this is a symptom of something else
being sub-optimal. Perhaps it's as simple as putting x and y into
a dictionary instead of creating them as class attributes:


I think a good reason for wanting to map string values into literal
variables/methods/classes such as Joseph explained, is so that they
can be dynamically defined in something such as a config file, for
example.

I wrote a simple application which had a basic text based
configuration file, and a key/value pair defined as:

MODULES = mod1, mod2, ..modN

.... where each module was seperated by commas, and read into the
application as strings. getattr() and __import__() proved very useful
at this point, allowing me to dynamically load the modules, and the
classes within using the config string values.
-- Brian Jones

Jul 18 '05 #3
What you really want is this:

class Foo:
x = "blah"
y = "blah some more"
foo = Foo()

x_string = 'x'
y_string = 'y'

print eval('foo.' + x_string)
print eval('foo.' + y_string)

Laughlin, Joseph V wrote:

class Foo:
x = "blah"
y = "blah some more"

foo = Foo()
x_string = "x"
y_string = "y"

# I want something like this to work:
print foo.x_string
print foo.y_string
# The above should print out "blah" and "blah some more"
# Any ideas?


Jul 18 '05 #4
What you really want is this:

class Foo:
x = "blah"
y = "blah some more"
foo = Foo()

x_string = 'x'
y_string = 'y'

print eval('foo.' + x_string)
print eval('foo.' + y_string)

Laughlin, Joseph V wrote:

class Foo:
x = "blah"
y = "blah some more"

foo = Foo()
x_string = "x"
y_string = "y"

# I want something like this to work:
print foo.x_string
print foo.y_string
# The above should print out "blah" and "blah some more"
# Any ideas?


Jul 18 '05 #5
On Fri, Jul 02, 2004 at 19:22:04 +0000, Tobiah wrote:
What you really want is this:

class Foo:
x = "blah"
y = "blah some more"


foo = Foo()

x_string = 'x'
y_string = 'y'

print eval('foo.' + x_string)
print eval('foo.' + y_string)


Using eval is overkill; something like:

print getattr(foo, x_string)
print getattr(foo, y_string)

should work just fine. In general, I'd suggest avoiding the use of eval
unless you really want something involving arbitrary code execution.
--
mithrandi, i Ainil en-Balandor, a faer Ambar

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA5b8TpNuXDQIV94oRAl7qAJwPDZIf7TcvbJuPDDW7zr 09d+TsiACeJUVN
TT2ucil39FMlUfxP9cnS8q4=
=Egqu
-----END PGP SIGNATURE-----

Jul 18 '05 #6

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

Similar topics

4
by: Andrew Chalk | last post by:
I am a raw beginner to Python. I just read in "Learning Python" that assigning to a string argument inside a function does not change the string in the caller. I want an assignment in the function...
2
by: mrambil | last post by:
Hi, I'm using PHP 4.3.3 in CLI on Win XP SP1. I use the _experimental_ w32api. It seems to work quite fine yet I cannot pass a string argument to any of my dll functions : 1) Load the w32api...
2
by: tmr_net | last post by:
I'm using the VC++ 2005 beta. I've seen several other suggestions on the message boards, but none even compiled for me....here is my solution: args is a managed String, and the first argument...
3
by: excelleinc.com | last post by:
Hi, I want to have a sub that takes string as argument and then opens a form that has that string as a class name. Say: Class users Inherits System.Windows.Forms.Form 'display form content
9
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string froma file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
6
by: damian | last post by:
Problem: I am looking to reduce my code size because I have many very simliar functions e.g.: private uint GenerateAcquisitionID(AcquisitionType c) { uint max = 0; foreach (AcquisitionType a...
1
by: PJOnions | last post by:
Hi, I'm trying to read in a string of the N=348, and convert it into an integer. I have already split the string down to remove the non numeric values and can print the correct value to the...
1
by: jijoja | last post by:
Hey there! Does anyone have a ready to plugin function that takes as an argument arbitrary long string and converts links written as a string into actual links. For example: Input string is:...
2
thatos
by: thatos | last post by:
I have this line of code in my program string line = "1"; I would like to convert line into a float, now I can't use float i = atof(line) because atof() takes in char * as its argument. Does...
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:
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: 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
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.