Differences between Ruby and Python in scope

What is a scope?

The range in which a variable can be used. If you try to use a variable that is out of scope, you will get an error.

Let's see an example where Ruby and Python actually have different scopes.

For Ruby

name = 'Mike'
def greeting()
  puts ('Hello ' + name)
end
greeting() #=> error (undefined local variable)

In Ruby, only the variables defined in the method can be used in the method. So this time, the greeting method is trying to use the out-of-scope variable name, which causes an error.

For Python

name = 'Mike'
def greeting():
  print('Hello ' + name)
  x = 2

greeting() #=> Hello Mike
print(x) #=> error

In Python, the variable name affects the greeting method, so it can be executed without error.

However, the variable x defined inside the method cannot be used outside the method, so an error occurs.

Recommended Posts

Differences between Ruby and Python in scope
Differences in authenticity between Python and JavaScript
Differences in syntax between Python and Java
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between Ruby and Python in terms of variables
Difference between Ruby and Python split
Difference between == and is in python
Differences between Python, stftime and strptime
Differences in string processing between Python, Ruby, JS, PHP (combination and variable expansion)
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Basic grammar] Differences between Ruby / Python / PHP
Difference in how to write if statement between ruby ​​and python
Ruby, Python and map
Python and Ruby split
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Summary of the differences between PHP and Python
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Python on Ruby and angry Ruby on Python
AtCoder ARC080 D simulation solved in Ruby and Python
[Ruby vs Python] Benchmark comparison between Rails and Flask
Python and ruby slice memo
Calculate Pose and Transform differences in Python with ROS
Ruby and Python syntax ~ branch ~
Stack and Queue in Python
Mutual conversion between JSON and YAML / TOML in Python
Difference between return, return None, and no return description in Python
Interprocess communication between Ruby and Python (POSIX message queue)
Unittest and CI in Python
Difference in writing method to read external source code between Ruby and Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and β€œ+” operator
I tried to enumerate the differences between java and python
AtCoder ABC168 A case expression solved in Ruby and Python
Summary of Differences Between Ruby on Rails and Django ~ Basics ~
Differences between Windows and Linux directories
Difference between java and python (memo)
MIDI packages in Python midi and pretty_midi
Differences between yum commands and APT commands
Scraping with Node, Ruby and Python
View photos in Python and html
Sorting algorithm and implementation in Python
Differences between symbolic links and hard links
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Cooperation between python module and API
Check and move directories in Python
Difference between python2 series and python3 series dict.keys ()
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
[Python] Difference between function and method
Reverse Hiragana and Katakana in Python2.7