Create a local scope in Python without polluting the namespace

It's a topic that may be scolded by devout Pythonistas, but in a situation where you want to do things in an embedded Python environment without polluting the namespace, I created a local scope using code like the one below. I've confirmed that it works with Python 2.7.4 / 3.3.1.

localscope.py


DummyClassForLocalScope = "global value"

try:
    class DummyClassForLocalScope:
        # you can write any code here

        raise RuntimeError("Exit from local scope")
except RuntimeError as exception:
    if exception.args != ("Exit from local scope",):
        raise exception

print(DummyClassForLocalScope)

I'm just canceling the class creation by creating a local scope with the class statement and raising an exception at the end of the local scope.

I don't want to use it a lot even if I make a mistake, but if you really want to do something like (function () {...} ()); in JavaScript, there is also such a method.

Recommended Posts

Create a local scope in Python without polluting the namespace
Create a function in Python
Create a Python image in Django without a dummy image file and test the image upload
Create a DI Container in Python
Create a binary file in Python
Create Gmail in Python without API
Create a Kubernetes Operator in Python
Create a random string in Python
Write the test in a python docstring
Create a simple GUI app in Python
Create a JSON object mapper in Python
Run the Python interpreter in a script
[GPS] Create a kml file in Python
How to easily create an environment where python code runs on Jupyter without polluting the local environment
Test & Debug Tips: Create a file of the specified size in Python
Create a record with attachments in KINTONE using the Python requests module
What is the fastest way to create a reverse dictionary in python?
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
[Python] Get the files in a folder with Python
Get the caller of a function in Python
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Make a copy of the list in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Output in the form of a python array
Create a package containing global commands in Python
[Python] Find the transposed matrix in a comprehension
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
A solution if you accidentally remove the python interpreter in / usr / local / bin /.
Create a Django project and application in a Python virtual environment and start the server
Create a Python module
Create SpatiaLite in Python
Create a Python environment
Check if the string is a number in python
Create a data collection bot in Python using Selenium
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[LINE Messaging API] Create a rich menu in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Write a log-scale histogram on the x-axis in python
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Solve the one-stroke writing (backtrack without recursion in Python)
A reminder about the implementation of recommendations in Python
Create a fake Minecraft server in Python with Quarry
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
What does the last () in a function mean in Python?
Create an exe file that works in a Windows environment without Python with PyInstaller
Take a screenshot in Python
Create a Wox plugin (Python)
Find out the apparent width of a string in python
Download the file in Python