Can Python Local & Global Variables in Function have the same name | Python Tutorials | Learn Python

Опубликовано: 21 Июнь 2018
на канале: The Perfect Tutorials
379
8

Know why it is best to keep the names different.

-----------------------------------------------------------------------------------------------------------
Can we have Python variables with the same name, one inside the function and the other outside it?
-----------------------------------------------------------------------------------------------------------
This is a never-ending confusion whether the local variables created in python can be used inside a function?
Well, yes. In this case, they become global variables when referenced inside a function.
And so, a function can have variables with the same name as variables present outside a function.
So when a function is called, it refers to the variable and the scope of that reference is resolved internally. That means, the variable that is local to the function will be referred.
For referring the variable outside the function, the keyword global has to be used.


Download the latest python release here: https://www.python.org/downloads/

Follow me on Instagram:   / the_perfect_tutorials  


На этой странице сайта вы можете посмотреть видео онлайн Can Python Local & Global Variables in Function have the same name | Python Tutorials | Learn Python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь The Perfect Tutorials 21 Июнь 2018, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 379 раз и оно понравилось 8 зрителям. Приятного просмотра!