Introduction to Object-Oriented Programming in Python
George Boorman
Curriculum Manager, DataCamp
Term | Definition |
---|---|
Class | A blueprint/template used to build objects |
Object | A combination of data and functionality; An instance of a class |
Term | Definition |
---|---|
Class | A blueprint/template used to build objects |
Object | A combination of data and functionality; An instance of a class |
State | Data associated with an object, assigned through attributes |
Behavior | An object's functionality, defined through methods |
Operator | Method |
---|---|
== |
__eq__() |
!= |
__ne__() |
>= |
__ge__() |
<= |
__le__() |
> |
__gt__() |
< |
__lt__() |
__str__()
print(obj)
, str(obj)
print([1,2,3])
[1 2 3]
str([1,2,3])
'[1, 2, 3]'
__repr__()
repr(obj)
, printing in consolerepr([1,2,3])
[1,2,3]
[1,2,3]
[1,2,3]
print()
class BalanceError(Exception): pass
class Customer: def __init__(self, name, balance): if balance < 0 : raise BalanceError("Balance has to be non-negative!") else: self.name, self.balance = name, balance
# Use try-except to catch errors try: cust = Customer("Larry Torres", -100) except BalanceError: cust = Customer("Larry Torres", 0)
Introduction to Object-Oriented Programming in Python