Skip to main content

My python utility functions

Project description

noglobal

This python package provides the @noglobal decorator which prohibits the use of global variables from the function scope.

Install

pip install noglobal

Usage

import os.path as osp
from noglobal import NoGlobal
noglobal = NoGlobal(globals()).noglobal

foo = 3

def bar():
    return "bar"

@noglobal
def f():
    print(osp.join("foo", "bar"))
    print(bar())
    print(foo)

f()
foo/bar
bar
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [1], in <cell line: 16>()
     13     print(bar())
     14     print(foo)
---> 16 f()

Input In [1], in f()
     12 print(osp.join("foo", "bar"))
     13 print(bar())
---> 14 print(foo)

NameError: name 'foo' is not defined

FAQ

How all this works?

The function with @noglobal decorator are re-created as a type.FunctionType object, with no global variables.

All globals are affected?

No. Althogh this package name is "noglobal", the globals below are not affected:

  1. modules
  2. callable objects (which means the objects which have __call__ method)

Thus you can not bother importing already imported modules everytime, and so on for the funcsions or classes.

Can we use explicit global variables?

No. Functions with the @noglobal decorator also disallow the explicit use of global variables. That is, if you update a variable bound by a global declaration within a function, the original global variable will not be affected. The following code prints 10 on the standard output.

bar = 10

@noglobal
def foo():
    global bar
    bar = 11
foo()
print(bar)

What happens for the nested functions?

Nested functions in the function decorated with @noglobal are also affected. For example, the code below returns error:

buzz = 11

@noglobal
def foo():
    def bar():
        print(buzz)
    bar()

foo()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [8], line 1
----> 1 foo()

Cell In [7], line 5, in foo()
      3 def bar():
      4     print(buzz)
----> 5 bar()

Cell In [7], line 4, in foo.<locals>.bar()
      3 def bar():
----> 4     print(buzz)

NameError: name 'buzz' is not defined

Attribution

Based on the source codes:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

noglobal-1.0.4.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

noglobal-1.0.4-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file noglobal-1.0.4.tar.gz.

File metadata

  • Download URL: noglobal-1.0.4.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for noglobal-1.0.4.tar.gz
Algorithm Hash digest
SHA256 8e91d0474855edeae8196f97467679e1d3a289e68746ee8f6108c5e06dc607b4
MD5 1fbf31248faadbb34aaae7bf02fed355
BLAKE2b-256 ddc2cd4041e4b147d569beac27f616972e0a1d6f810344032888f63b47197577

See more details on using hashes here.

File details

Details for the file noglobal-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: noglobal-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for noglobal-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2cf40ce43fe2946b6600bb20d905e573bc90bb2249335dc5d23d6ff2b8a03772
MD5 7807c2ff537a2fd54dd44ba764121c16
BLAKE2b-256 94f8906d063d8467c8f56c06dfeeb443928d91d227a1f4b2b050b561ab398f6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page