473,594 Members | 3,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1544
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.pytho n 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.pytho n 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)

iD8DBQFA5b8TpNu XDQIV94oRAl7qAJ wPDZIf7TcvbJuPD DW7zr09d+TsiACe JUVN
TT2ucil39FMlUfx P9cnS8q4=
=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
13536
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 to alter the passed string in the caller. Is there any way to do this? For example def SafeAdd(self, Variable, Value): if self.form.has_key( Value ): Variable = self.form.value
2
1727
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 extension : if (!dl("php_w32api.dll")) { echo "Erreur : Impossible de chager le module php_w32api (php_w32api.dll)."; exit;
2
1492
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 passed into my program. It is supposed to be a filename. I have a legacy library which is unmanaged code that requires an old c-style char array. System::Text::ASCIIEncoding ascii_enc; int len =...
3
1501
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
2726
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
3994
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 in c) { uint n = Convert.ToUInt32(a.acquisitionID);
1
2805
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 screen. When I then try to convert the string of numeric values to an int using the atoi function the code won't compile and comes up with the error below. A small bit of the code is included below the error message, any help would be appreciated. I...
1
5421
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: Please, visit our website http://www.thescripts.com and find out more about programmers community. You can also go to http://www.thescripts.com/forum/archive/index.php/f-130.html if you need help in yari, yari, yari. Output of the function should be:...
2
15368
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 someone know how can I convert a string into a float or at least convert type string to char * then use atof().
0
7947
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
7880
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8010
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5413
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.