Giter Club home page Giter Club logo

scoping's Introduction

Probably the best way to simulate block scopes in Python.

This is a package, as it says on the tin, to emulate block scoping in Python, the lack of which being a clever design choice yet sometimes a trouble.

In addition to readability and code organization (where your mileage may vary), block scoping in particular helps to have variables garbage collected as soon as possible, which is useful for situations where variables may refer to expensive resources (e.g. GPU arrays).

This package is designed to be as easy to use as possible, with the least mental burden on the user, whilst the implementation being necessarily confusing and cryptic due to the tricks used.

Usage

Other than installing from PyPI the usual way, you can also directly download scoping.py to where your main script is to use the package.

To start a scoped block (where the variables created in the block is to be deleted after the block) use

with scoping():
    #scoped code here........

Within a block, you can selectively let a variable leak through to the outer scope (as in the traditional behavior in Python) by passing the name (as a string) to

scoping.keep()

If you are only after using the library, just consider scoping and scoping.keep as some kind of quasi-keywords rather than real classes/objects.

See

from scoping import scoping
a = 2
with scoping():
    assert(2 == a)
    a = 3
    b = 4
    scoping.keep('b')
    assert(3 == a)
assert(2 == a)
assert(4 == b)

In the rare case that you would like to reserve the word scoping for other uses, the class can be renamed arbitrarily using the following trick:

import scoping
class VeryScoped(scoping.scoping): pass
del scoping

Then the names VeryScoped and VeryScoped.keep can be used instead, whereas the name scoping can be put to other uses at will.

Blocks can be nested, as well as used in functions (unlike prior art), at the price of relying on some CPython specific feature.

Motivation/Prior Art

See https://github.com/bskinn/tempvars with a similar idea but not the intended use for general programming, as well as a more mentally demanding interface (IMHO).

Relationship with JavaScript let

This functionality is also similar to JavaScript let keyword if you consider the default behavior to be the JavaScript var, though neither comparison would be a perfect match. Now that JavaScript has its let, I would look forward to this making its way into the main language, as the same arguments for let in JavaScript apply to the adoption of this package in Python.

Compatibility

Tested with python 3.6/7/8/9 under Linux

scoping's People

Contributors

l74d avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.