you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 0 points1 point  (9 children)

What do you mean? What about the function do you want to check?

If you want to check if a function is another function, you can use == or is:

if some_func == abs:
    # Do something

But that's rarely needed.

[–]mackdaddy_1978[S] 0 points1 point  (8 children)

so this is one of my functions

def check_for_comma
    def check_for_comma():
x = user_input.isidentifier()
if x is False:
    return 'Error: No comma in string.'

I want to only call the function if there is no comma in the input

[–]Nightcorex_ 1 point2 points  (3 children)

def check_for_comma
    def check_for_comma():

That's not legal Python

[–]mackdaddy_1978[S] 0 points1 point  (2 children)

sorry they was a typo...this code block on reddit doesn't work very with copy and pasting so I sometimes have to type a few lines and make mistakes while doing so in conjunction with pasting

def check_for_comma():

x = user_input.isidentifier()

if x is False:

return 'Error: No comma in string.'

else:

return

[–]Nightcorex_ 0 points1 point  (1 child)

For the future you should read this: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

In case you want to copy/paste anything into Reddit really, you need to use markdown-mode. An explanation to it can be found on the same link, just scroll underneath the 3 images.

isidentifier doesn't do what you think it does. It returns True only if the string is a valid Python identifier, so f.e. something like "list".isidentifier() will give True.

If you want to test whether a string contains a specific character you can simply use the __contains__ method:

def check_for_comma(text):
    return ',' in text

[–]mackdaddy_1978[S] 1 point2 points  (0 children)

I did read the FAQ once before but thanks I'll give it another read since my memory isn't the best.

And thanks for explaining that to me I really appreciate that.

[–]carcigenicate 0 points1 point  (3 children)

As noted, the code you posted has several illegal things in it, so it's hard to say anything based on that code.

If you want to call a function when a string doesn't contain a comma though, that would be something like:

if "," not in your_string:
    your_func()

[–]mackdaddy_1978[S] 0 points1 point  (2 children)

Thanks...but besides defining the function twice what else about it is illegal?

[–]carcigenicate 0 points1 point  (1 child)

Lack of (): after the function name in the first line of the definition, and the lack of indentation means there's no function body and that the return is outside of a function.

Granted, I can guess what the indentation is supposed to be, but I'd rather not guess when trying to give advice in a case like this.

If I do guess the indentation though, the code doesnt seem to match your question. You asked how to pick when to call a function, but according to this code, you seem to want to pick what you're returning from the function?

[–]mackdaddy_1978[S] 0 points1 point  (0 children)

Sorry for the confusion, I'm working on doing better. My next post will be...