[Tutor] challenge-chapter 2

Steven D'Aprano steve at pearwood.info
Sat May 25 04:57:30 CEST 2013


On 25/05/13 09:41, bob gailer wrote:
> On 5/22/2013 1:02 AM, Andrew Triplett wrote:
>> ... illegal variable names ...
> That is a very poor description. Something can either be used as a variable name or not.
>
> abc can be used as a variable name, if can't.

But it can be used as a variable name in some languages. "if" is at least *potentially* a variable name, in the sense that somebody might try to use it as a name.


> I defy you to give me a variable name that is illegal! Anything you attempt to give in that category is not a variable name.

While you are strictly correct, you are missing the point of the exercise, which is to distinguish between strings which can be used as variable names:

pedant
variable
constant
this_is_not_a_variable
pi
tau
alpha1234


and those that can't:

1234alpha
*$%^
for
return
this-is-a-variable


A better name for this is "identifier" rather than "variable name", and Python 3 defines a string method to test whether or not something is a valid identifier:

http://docs.python.org/3/library/stdtypes.html#str.isidentifier


-- 
Steven


More information about the Tutor mailing list