isolated-sandbox / examples /python_classes.py
ChefAdorous's picture
Deploy Code Execution Sandbox with FastAPI and Docker
a89f25d
# Python Example: Classes and Object-Oriented Programming
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
self.transactions = []
def deposit(self, amount):
if amount > 0:
self.balance += amount
self.transactions.append(f"Deposited: ${amount}")
return True
return False
def withdraw(self, amount):
if amount <= self.balance and amount > 0:
self.balance -= amount
self.transactions.append(f"Withdrew: ${amount}")
return True
return False
def get_balance(self):
return f"${self.balance}"
def show_transactions(self):
print(f"Transaction history for {self.owner}:")
for transaction in self.transactions:
print(f" - {transaction}")
# Create account
account = BankAccount("John Doe", 1000)
print(f"Account created for {account.owner}")
print(f"Initial balance: {account.get_balance()}")
# Perform transactions
account.deposit(500)
account.withdraw(200)
account.deposit(100)
print(f"Current balance: {account.get_balance()}")
account.show_transactions()