# Introduction to Computation Using Python ## Lesson 7
Expedia Code Academy
### OOP - Object Oriented Programming Object-oriented programming (OOP) is a programming model based on the concept of "objects", which may contain data, in the form of attributes and their behavior in the form of methods.
### Define a class with a constructor Class variable values are shared among all instances of this class. This can be accessed as `Member.class_variable` from inside or outside the class. The first method `__init__()` is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. ``` python class Member: "It's a member class" # documentation string total_members = 0 # Class variable def __init__(self, name, age, is_captain, total_step_count): # initialize method called when class object is created self.name = name self.age = age self.is_captain = "Yes" if is_captain else "No" # assign "Yes" if the is_captain value is True else assigns "No" self.total_step_count = total_step_count Member.total_members += 1 # total_members is increased by 1 every time a new instance of Member is created ```
### Define a class method You declare other class methods like normal functions with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you do not need to include it when you call the methods. ``` python class Member: "It's a member class" # documentation string total_members = 0 # Class variable ... def print_details(self): # Class method print("Member:", self.name, " Team:", self.team_name, " Total Steps:", self.total_step_count, "Captain:", getattr(self, "is_captain"), "\n") #getattr method here returns the value of is_captain. it's an alias for self.is_captain ```
### Istantiate a class To create instances of a class, you call the class using class name and pass in whatever arguments its init method accepts. ``` python object_name = Class_Name(args...) ``` For example: ``` python john = Member("John", "18", "Safire", True, 500000) # Member object creation john.print_details() print(Member.total_members) ```