[Tutor] using global variables with functions

Daniel Ehrenberg littledanehren at yahoo.com
Fri Dec 5 21:02:27 EST 2003


"Dutcher, John" wrote:
> 
> 	Hello Tutor,
> 
> 	Can you comment on this issue ?
> 
> 	Using Python 2.3.1, I carefully import 'string'
> along with other
> modules.
> 
> 	The interpreter will NOT let me use any 'string'
> functions in the
> code,
> 	(i.e. upper(), lower(), capitalize() etc.)
> 
> 	It indicates no 'global string' defined.
> 
> 	In a couple other scripts I coaxed it to let me use
> 'string.rstrip'
> but
> 	ONLY by doing away with a couple user functions I
> had positioned
> 	at the top of the script....and handling the same
> code 'inline'.
> 
> 	I hate to be locked into that mode always. I assume
> there is some
> 	fundamental concept that relates to using your own
> 'defs', and how
> 	they confuse the interpreter and prevent it from
> recognizing the
> 	import of  'string'. The concept of having 'masked'
> it away in my
> 	own code arises I guess....but I have no variables
> named 'string'
> 	or named any of the 'string' functions either ?????
> 
> 	Thanks,     John Dutcher 

The string module is obsolete, you shouldn't use it.
There are instead new string methods that can be used
by treating the string as an object. Here's some code
to demonstrate the string functions:

>>> test_string = '  thisIs a String'
>>> test_string.upper()
'  THISIS A STRING'
>>> test_string.lower()
'  this is a string'
>>> test_string.join(['a', 'b', 'c'])
'a  thisIs a Stringb  thisIs a Stringc'
>>> test_string.split(' ')
['', '', 'thisIs', 'a', 'String']
>>> test_string.strip() #does both sides
'thisIs a String'

and so on. Most of the methods in the string module
are still available in the string methods (except some
are at different names), but the constants, such as
string.printable, are not. See string.__doc__ for more
details.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/



More information about the Tutor mailing list