I Used Global Variables for “Convenience” (And Created Bugs I Couldn’t Reproduce)
Author(s): Dua Asif Originally published on Towards AI. AI GENERATED # config.pyDATABASE_URL = "postgresql://localhost/mydb"API_KEY = "sk_live_abc123"DEBUG = True# app.pyimport configdef connect_database(): return psycopg2.connect(config.DATABASE_URL)def call_api(endpoint): return requests.get(f"https://api.example.com/{endpoint}", headers={'X-API-Key': config.API_KEY}) Clean. Simple. Every module could access configuration through import config. No passing parameters everywhere. …
I Thought Private Variables Were Actually Private (Then I Accessed Them From Outside the Class)
Author(s): Dua Asif Originally published on Towards AI. AI GENERATED class BankAccount: def __init__(self, balance): self.__balance = balance # Private variable def get_balance(self): return self.__balance def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount return True return False Two underscores. That …