Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1.37 KB

File metadata and controls

39 lines (31 loc) · 1.37 KB

Objects

What are objects?

  • Objects are entities created by Python
    • they have state (data)
    • they have methods (functionality)
    • state and methods are encapsulated into Objects
  • Integers (int) are Objects
    • Integers (int) have state - the value of the integer
    • Integers (int) also have functionality
      • knows how to add itself to another integer
      • knows how to represent itself as a string (for visual output)
  • Float (float) numbers are objects
    • state -> value
    • functionality -> add

Everything in Python is an Object

  • Any data type we have in Python is an object
    • It has a state
    • It has functionality
    • These states/ functionality are called attributes

Dot Notation

  • Is used to access attributes of an object
    • car.brand -> accesses the brand attribute of the car object
    • car.model -> accesses the model attribute of the car object
  • For attributes that represent functionality, we usually have to call the attribute to perform the action -> often supplying additional information (parameters)

Mutability

  • An object is mutable if its internal state (data) can be changed
    • One or more data attributes can be changed
    • for example: integers, floats, booleans...
  • An object is immutable if its internal state cannot be changed
    • The state of the object is "set in stone."
    • for example: lists, dictionaries, sets...