Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  • Definition of Namespace
  • Types of Namespace
  • Characteristics of Namespace
  1. Home
  2. Languages
  3. Python

Namespace in Python

Definition of Namespace

The scopes that distinguishes a particular objects by name

Mapping between names and actual objects

If the namespaces belong to are different, it becomes possible to have the same name point to different objects

Generally in Python, configuration of main module is as follows.

def main():
  pass 

if __name__ == '__main__':
  main()
  # do something ... 

Actually, Python is script language. So there is no main function that execute automatically.

If we once run the module, all non-indented code (Lv 0 code) is executed, and function and classes are defined only and not executed.

Therefore, as in the example above, it may be difficult to find reason to define the main function and execute it with the if statement below.

However, there is a reason why most codes follow the above format.


Types of Namespace

Python's namespace can be classified into three categories

Global namespace

Exists for each module, and names of global variables, functions or classes are included here.

a = 3
print(globals())

>> {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x1046110f0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/namespace.py', '__cached__': None, 'a': 3}

Local namespace

Exists for each function or method, and names of local variables in functions are included here.

a = 3

def func():
  a = 100
  b = 'python'
  print
  def printer():
    print('hello')

func()
print(globals())

>> outer_func namespace {'v': 'world', 'a': 100, 'b': 'python'}
>> inner_func namespace {}

Built-in namespace

Names of basic built-in function and basic exceptions belongs here. Includes all scopes of code written in Python.


Characteristics of Namespace

Python’s namespace has the following characteristics.

  1. All namespaces are implemented by shape of dictionary.

  2. All names consists of strings themselves, each of which points to an actual object in the scope of its own namespace.

  3. Since the mapping between the name and the actual object is mutable. Therefore, new name can be added during runtime.

  4. But, built-in namespaces cannot be deleted or added arbitrarily.


PreviousDecorator in PythonNextPython Method

Last updated 1 year ago

📖