2

Is there a way to do this in python?:

def exist(path_image):
    if path.isfile(path_image):
       return path_image
    else:
       return False

path_image = '/home/user/image.jpg'
if (image = exist(path_image)):
   print(image)

Other languages like PHP and Javascript, I can initialize a variable inside 'IF'

Thanks

3
  • you can't do this. Python was constructed to avoid this. Commented Dec 15, 2017 at 5:39
  • you could make it a side effect of a function call. tho, it's not good idea. Commented Dec 15, 2017 at 5:47
  • No, Python on purpose makes assignment not an expression. A PEP was created proposing this, and it was never accepted: python.org/dev/peps/pep-0379 Commented Dec 15, 2017 at 6:06

1 Answer 1

1

No, you cannot do this in Python. A simple alternative would be:

image = exist('/home/user/image.jpg')
if (image):
   print(image)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.