Skip to content
Adewunmi Oladele edited this page Feb 8, 2023 · 1 revision

Welcome to the AirBnB_clone wiki!

Object oriented programming Object Oriented Programming principles Everything wihitn Python is an object Python dose not requiere you to use object or classes Complex programs are hard to keep organized OOP organizes and structures code Groping related data and behavior into one place to reduce bugs and ensure that only the parts of the program that need access to the data are actually accessing it. OOP also promotes building modular code Isolates parts of the program that need to be changed Class is a blueprint for creating objects of a particular type Methods refer to regular functions that are part of a class Atributes are variables that hold data that are part of a class Object is an instance of a class Number 1 is an instance of an integer type Inheritance describes how can arrange classes in a hierarchical fasion where a particular class can inherit the capabilities and data of another class. Composition refers to building complex objects out of other objects Class definition Create a simple class called Book that has three attributes: title, author, and pages. class Book(): """docstring""" def init(self, title, author, pages, price): self.title = title self.author = author self.pages = pages self.price = price

The init() function is called to initialize the new object with information, and is called before any other functions defined in the class. This is the initilizer function. self is a reference to the current instance of the class, and is used to access variables that belong to the class. The class keyword is used to define a new class follow by the name of the class. The class name should be in CamelCase and requieres to use () only if class inherits from another class. To create instances of the class, use the () to call the class and pass in the arguments that are required. book1 = Book('Python Rocks', 'Ronald Alexander', 200, 39.99) book2 = Book('Python for Beginners', 'Rodrigo Mato', 100, 49.99)

This is an example of a class definition that creates two instances (objects) of the class Book. print(book1) print(book1.title) print(book1.author) print(book1.pages) print(book1.price)

<main.Books object at 0x7f85a5be2d00> Python Rocks Ronald Alexander 200 39.99

Instances methods and attributes Create an instance method to print the price of the book. def getprice(self): return self.price

print the price of the book print("Price: {:.2f}".format(book1.getprice()))Price:39.99

Clone this wiki locally