[Tutor] valueOf() equivalent

Darrell Brogdon darrell@brogdon.net
Thu, 21 Dec 2000 21:39:39 -0500


Thanks for the help.  In working on my code I found I didn't need to do 
this after all.  But the suggestions were helpful and provided good insight.

Another unrelated question though:  Is there a requirement to close an 
"os.popen()" connection?  I've checked the docs and haven't found 
anything.  If not then is it safe to assume that Python automatically 
closes these connections when the script ends?  I just want to make sure 
I'm not leaving a bunch of open connections in my script.

Daniel Yoo wrote:

> On Tue, 19 Dec 2000, Darrell Brogdon wrote:
> 
> 
>> Well, in Java you have the valueOf() method which, as its name sounds, 
>> will tell you the value of a member (variable).  So in my example I 
>> could say:
>> 
>>    print tmp_var.valueOf()
>> 
>> ...if Python were to have a valueOf() function.
> 
> I think you're referring to Java's class wrappers over the primitive data
> types (integers, doubles, floats, chars...)
> 
> Although Python's integers are objects (reference counted and all), you
> can just get the value of them by using the variable --- they're
> "primitive" in this sense.  However, if you really want to use them, there
> are class wrappers for these types:
> 
> UserList (class wrapper over lists):
>     http://python.org/doc/current/lib/module-UserList.html
> 
> UserDict (class wrapper over dictionaries):
>     http://python.org/doc/current/lib/module-UserList.html
> 
> UserString (class wrapper over strings):
>     http://python.org/doc/current/lib/module-UserString.html
> 
> In these three, you'd use the .data member to get at the value of the
> object.  However, you will most likely not need to use these classes ---
> they're there if you want to extend their functionality through
> inheritence.  Instead, just treat numbers and strings as primitives, and
> you should be ok.
> 
> 
> 
>>            eval('self.my_var') = my_test_var   # <-- I know this won't 
>> work but this is why I need a "valueOf()" equivalent.
> 
> 
> I believe you can do this with:
> 
>     self.my_var = my_test_var
> 
> Just stick it in there.  There's no need to use a valueOf(), since Python
> variables have no static type to worry about --- class instances are
> dynamic enough that you can just start stuffing extra variables into
> them.  For example:
> 
> ###
> class EmptyClass: pass
> 
> x = EmptyClass()
> x.var1 = 42
> x.name = 'Darrell'
> ###
> 
> should be legal (as long as I didn't misspell anythign... *grin*)
> 
> 
> 
>> If you're familiar with PHP, this can be considered using a variable 
>> variable (http://php.net/manual/language.variables.variable.php).
> 
> Reading through the php manual, this idea of "variable-named" variables
> sounds like you want to use either eval() for expressions or exec() for
> statements.  Your statement above, then, could be expressed like this:
> 
>     exec("self.my_var = my_test_var")
> 
> (But of course, if you were going to write this, you would do the more
> direct statement.  I'm assuming you want to do some really dynamic code,
> perhaps by dynamically generating a statement out of a string?)
> 
> 
> If you're interested with dynamic variable stuff, take a look here:
> 
>     http://python.org/doc/current/lib/built-in-funcs.html
> 
> The functions getattr() and setattr() should be very interesting to
> you: they let you add arbitrary "attributes" into a class instance, using
> a normal string as a name.  So you can do stuff like:
> 
>     setattr(self, "my_" + "var", my_test_var)
>     setattr(self, "my_" + "var", eval("my_test_var"))
> 
> although it makes me feel a bit nervous seeing this sort of programming
> with wild abandon.
> 
> 
> 
>>>> myTestFunction('test_variable', 5)
>>> 
> To make this work, you'd do:
> 
>     myTestFunction(eval('test_variable'), 5)
> 
> because the string 'test_variable' needs to be evaluated somehow ---
> otherwise, you're just passing the string 'test_variable' to your
> myTestFunction().
> 
> However, there are probably more direct ways of writing your program; you
> may find you don't need to use as many dynamically named variables.  Tell
> us what you're writing, and we can make suggestions on Python idioms for
> doing something equivalent.
> 
> 
> I hope some if this made sense.  If you have more questions or
> clarifications, please feel free to ask.
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor


-- 
Darrell Brogdon
http://darrell.brogdon.net