0

Assuming we have a method returns_true() which just returns True. Is it possible to distinguish, from inside a foo() method with a single argument, if returns_true() was an argument or a True Literal? (purely motivated by curiosity, not as a solution to any particular problem)

def foo(obj):
    pass # ? 

def returns_true():
    return True

foo(True)
foo(returns_true())
egeres
  • 77
  • 3
  • 9
  • Does this answer your question? [Python function overloading](https://stackoverflow.com/questions/6434482/python-function-overloading) – Harun Yilmaz Nov 03 '21 at 12:57
  • 7
    Your title says "*a method*", but the 2nd call is _not_ passing a method, but the _return value_ of `returns_true()` which is also the same as `True`? What problem are you solving here? – Gino Mempin Nov 03 '21 at 12:58
  • 1
    @HarunYilmaz It's a different question – Yevhen Kuzmovych Nov 03 '21 at 12:59
  • The answer is NO (from inside of `foo`). – Yevhen Kuzmovych Nov 03 '21 at 12:59
  • 1
    It's possible, but only if you parse the file. Generally, [this wouldn't be needed](https://meta.stackexchange.com/q/66377), so you need to provide more details on what problem you're trying to solve. – enzo Nov 03 '21 at 12:59
  • Not really possible and neither should it be possible. *How* a method is called must not matter. – luk2302 Nov 03 '21 at 13:02
  • It may not be *advisable*, but it is *possible*, to some degree: [Python to get the variable name outside the function call](https://stackoverflow.com/questions/54121891/python-to-get-the-variable-name-outside-the-function-call) If you explain *why* you would want to do this, you might get better answers. – Aleksi Torhamo Nov 03 '21 at 13:03
  • 1
    Yes, `returns_true()` would indeed return a plain `True` instance, and I know a way to distinguish if one was inputted or the other would be to just make `returns_true()` return something of a different type. But I was wondering if, under the assumption that in both cases we would have a `True` value, we can make a distinction – egeres Nov 03 '21 at 13:03
  • 1
    In your example, there is no distinction to make, because `foo` receives the literal `True` in both cases, unless `returns_true` returns something else different than the literal `True`. You will have to provide a clearer example. – Gino Mempin Nov 03 '21 at 13:06

1 Answers1

0

I found a hacky solution which takes some ideas from this answer referenced by Aleksi

import inspect

def returns_true():
    return True

def foo(input):
    print (inspect.stack()[1].code_context[0].split("(", 1)[1][:-2])

foo(True)           # >>> 'True'
foo(returns_true()) # >>> 'returns_true()'
egeres
  • 77
  • 3
  • 9