πŸ”
Tech
  • 🟒App aspects
    • Software architecture
      • Caching
      • Anti-patterns
      • System X-ability
      • Coupling
      • Event driven architecture
        • Command Query Responsibility Segregation (CQRS)
        • Change Data Capture (CDC)
      • Distributed transactions
      • App dev notes
        • Architecture MVP
      • TEMP. Check list
      • Hexagonal arch
      • Communication
        • REST vs messaging
        • gRPC
        • WebSocket
      • Load balancers
      • Storage limits
      • Event storming
    • Authentication
    • Deployment strategy
  • Databases
    • Classification
    • DB migration tools
    • PostreSQL
    • Decision guidance
    • Index
      • Hash indexes
      • SSTable, LSM-Trees
      • B-Tree
      • Engines, internals
    • Performance
  • System design
    • Interview preparation
      • Plan
        • Instagram
        • Tinder
        • Digital wallet
        • Dropbox
        • Live video streaming
        • Uber
        • Whatsup
        • Tiktok
        • Twitter
        • Proximity service
    • Algorithms
    • Acronyms
  • 🟒Programming languages
    • Java
      • Features
        • Field hiding
        • HashCode() and Equals()
        • Reference types
        • Pass by value
        • Atomic variables
      • Types
      • IO / NIO
        • Java NIO
          • Buffer
          • Channel
        • Java IO: Streams
          • Input streams
            • BufferedInputStream
            • DataInputStream
            • ObjectInputStream
            • FilterInputStream
            • ByteArrayInputStream
        • Java IO: Pipes
        • Java IO: Byte & Char Arrays
        • Java IO: Input Parsing
          • PushbackReader
          • StreamTokenizer
          • LineNumberReader
          • PushbackInputStream
        • System.in, System.out, System.error
        • Java IO: Files
          • FileReader
          • FileWriter
          • FileOutputStream
          • FileInputStream
      • Multithreading
        • Thread liveness
        • False sharing
        • Actor model
        • Singleton
        • Future, CompletableFuture
        • Semaphore
      • Coursera: parallel programming
      • Coursera: concurrent programming
      • Serialization
      • JVM internals
      • Features track
        • Java 8
      • Distributed programming
      • Network
      • Patterns
        • Command
      • Garbage Collectors
        • GC Types
        • How GC works
        • Tools for GC
    • Kotlin
      • Scope functions
      • Inline value classes
      • Coroutines
      • Effective Kotlin
    • Javascript
      • Javascript vs Java
      • TypeScript
    • SQL
      • select for update
    • Python
      • __init.py__
  • OS components
    • Network
      • TCP/IP model
        • IP address in action
      • OSI model
  • 🟒Specifications
    • JAX-RS
    • REST
      • Multi part
  • 🟒Protocols
    • HTTP
    • OAuth 2.0
    • LDAP
    • SAML
  • 🟒Testing
    • Selenium anatomy
    • Testcafe
  • 🟒Tools
    • JDBC
      • Connection pool
    • Gradle
    • vim
    • git
    • IntelliJ Idea
    • Elastic search
    • Docker
    • Terraform
    • CDK
    • Argo CD
      • app-of-app setup
    • OpenTelemetry
    • Prometheus
    • Kafka
      • Consumer lag
  • 🟒CI
    • CircleCi
  • 🟒Platforms
    • AWS
      • VPC
      • EC2
      • RDS
      • S3
      • IAM
      • CloudWatch
      • CloudTrail
      • ELB
      • SNS
      • Route 53
      • CloudFront
      • Athena
      • EKS
    • Kubernetes
      • Networking
      • RBAC
      • Architecture
      • Pod
        • Resources
      • How to try
      • Kubectl
      • Service
      • Tooling
        • ArgoCD
        • Helm
        • Istio
    • GraalVM
    • Node.js
    • Camunda
      • Service tasks
      • Transactions
      • Performance
      • How it executes
  • 🟒Frameworks
    • Hibernate
      • JPA vs Spring Data
    • Micronaut
    • Spring
      • Security
      • JDBC, JPA, Hibernate
      • Transactions
      • Servlet containers, clients
  • 🟒Awesome
    • НСйробиология
    • Backend
      • System design
    • DevOps
    • Data
    • AI
    • Frontend
    • Mobile
    • Testing
    • Mac
    • Books & courses
      • Path: Java Concurrency
    • Algorithms
      • Competitive programming
    • Processes
    • Finance
    • Electronics
  • 🟒Electronics
    • Arduino
    • IoT
  • Artificial intelligence
    • Artificial Intelligence (AI)
  • πŸš€Performance
    • BE
  • πŸ“˜Computer science
    • Data structures
      • Array
      • String
      • LinkedList
      • Tree
    • Algorithms
      • HowTo algorithms for interview
  • πŸ•ΈοΈWeb dev (Frontend)
    • Trends
    • Web (to change)
  • πŸ“ˆData science
    • Time series
Powered by GitBook
On this page
  • What is __init__.py?
  • Project Layout
  • πŸ“„ mathlib/calculator.py
  • πŸ“„ mathlib/geometry.py
  • πŸ“„ mathlib/__init__.py
  • πŸ“„ main.py
  • β–Ά How to Run

Was this helpful?

  1. Programming languages
  2. Python

__init.py__

What is __init__.py?

__init__.py is a special file that tells Python:

β€œThis folder is a package β€” it can be imported.”

Without it (in older Python versions), Python would not treat a directory as importable.

Project Layout

mathlib_project/
β”œβ”€β”€ main.py
└── mathlib/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ calculator.py
    └── geometry.py

πŸ“„ mathlib/calculator.py

class Calculator:
    def add(self, a, b):
        return a + b

    def subtract(self, a, b):
        return a - b

πŸ“„ mathlib/geometry.py

def area_of_circle(radius):
    pi = 3.14159
    return pi * radius * radius

def perimeter_of_square(side):
    return 4 * side

πŸ“„ mathlib/__init__.py

# Re-export classes/functions from submodules
from .calculator import Calculator
from .geometry import area_of_circle, perimeter_of_square

Now you can just do from mathlib import Calculator instead of from mathlib.calculator import Calculator.


πŸ“„ main.py

from mathlib import Calculator, area_of_circle, perimeter_of_square

def main():
    calc = Calculator()
    print("2 + 3 =", calc.add(2, 3))
    print("9 - 4 =", calc.subtract(9, 4))

    print("Area of circle (r=5):", area_of_circle(5))
    print("Perimeter of square (s=3):", perimeter_of_square(3))

if __name__ == "__main__":
    main()

β–Ά How to Run

Make sure you're in the mathlib_project/ directory, then run:

python main.py

βœ… Output

2 + 3 = 5
9 - 4 = 5
Area of circle (r=5): 78.53975
Perimeter of square (s=3): 12
PreviousPythonNextOS components

Last updated 5 days ago

Was this helpful?

🟒