Converting string to argument

  • Thread starter Laughlin, Joseph V
  • Start date
L

Laughlin, Joseph V

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
 
H

Heather Coppersmith

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
 
B

Brian Jones

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
 
T

Tobiah

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)
 
T

Tobiah

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)
 
T

Tristan Seligmann

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)

iD8DBQFA5b8TpNuXDQIV94oRAl7qAJwPDZIf7TcvbJuPDDW7zr09d+TsiACeJUVN
TT2ucil39FMlUfxP9cnS8q4=
=Egqu
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top