3

I'm working through a Python 3 book and came across the string function isidentifier(). The text description is "s.isidentifier() : Returns True if s is non empty and is a valid identifier". I tested it in the Python Shell like this:

>>> s = 'test'
>>> s.isidentifier()
True
>>> 'blah'.isidentifier()
True

I would expect the 2nd statement to return false since 'blah' is not held in a variable. Can anyone explain this? Thanks.

4 Answers 4

12

Returns True if s is non empty and is a valid identifier.

What they mean is that s could be valid as an identifier. It doesn't imply that it is an identifier that's in use.

Your first example is showing the same thing: 'test' (what isidentifier is actually checking) is not the name of a variable either. I think you meant

>>> 's'.isidentifier()
True
Sign up to request clarification or add additional context in comments.

3 Comments

To write what rosscj2533 thought it was, I guess you could do isidentifier() first and then do an eval() and see if it gives you a NameError or not.
Ah, I was taking it as is currently a valid identifier that's in use. Thanks.
@MatrixFrog - much better to test for existence of this identifier in locals() and globals(), rather than eval()'ing it. Faster and safer.
5

"isidentifier" doesn't say anything about the "variable" the string being tested is referenced by. So

'blah'.isidentifier()

is identical to

s = 'blah'
s.isidentifier()

In Python, it's never (or rarely) about the "variable" (Python doesn't have variables), it's about the object. In this case, strings.

Comments

5

Python doesn't have "variables". It is more helpful to think in terms of objects.

'blah' definitely exists at the time 'blah'.isidentifier() is called (after all it means that "call isidentifier() method of the string object 'blah'").

So if your understanding were correct, isidentifier() method of string objects should always return True, because at the time of the call, the object definitely exists.

What isidentifier() does is to check that the string object can be used as a valid identifier. Try these two lines in your Python session for example:

>>> a = "$"
>>> "$".isidentifier()

Even though "$" is assigned to the name a, the isidentifier() call returns False since $ is not a valid identifier in Python.

Comments

5

isidentifier is a Python function that simply tests whether a string contains only certain characters (underscore, digits, and alphas) and starts with an alpha or an underscore, so the string can be used for a valid Python identifier. Other functions that test for character classes are isalpha, isalnum, isdigit, and others.

ss = (
    'varABC123',
    '123ABCvar',
    '_123ABCvar',
    'var_ABC_123',
    'var-ABC-123',
    'var.ABC.123',
    # check your own strings
)

fmt = '%-15s%-10s%-10s%-10s%-10s' 
print(fmt % ('', 'isalpha', 'isalnum', 'isdigit', 'isidentifier'))
for s in ss:
    print(fmt % (s, s.isalpha(), s.isalnum(), s.isdigit(), s.isidentifier()))

Result:

               isalpha   isalnum   isdigit   isidentifier
varABC123      False     True      False     True      
123ABCvar      False     True      False     False     
_123ABCvar     False     False     False     True      
var_ABC_123    False     False     False     True      
var-ABC-123    False     False     False     False     
var.ABC.123    False     False     False     False     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.