Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

What is Object oriented programming, classes and objects in Python

$
0
0

Object oriented programming or OOP is a computer programming paradigm where data and related functions are encapsulated together. This is the first part of my OOP section. Checkout the full course: https://bit.ly/2v7lT74

:

# Global varible
restaurant_name = '7 Eleven'
restaurant_owner = 'Rahim'
def restaurant_details(): # function
print (restaurant_name , restaurant_owner)
def another_restaurant():
# Local variable
restaurant_address = 'Bogra'
print (restaurant_name , restaurant_owner)
print (restaurant_address)
restaurant_details()
another_restaurant()
# print (restaurant_address) # shows error
# print (restaurant_name)
# Syntax
'''
class ClassName (ParentClass):
class variables
instance methods
'''
class Restaurant: # () parentheses optional
def details(self): # instance method
print (self.name , self.owner)
def details_with_address(self, address):
# Local variable
self.details()
print (address)
## Instantiation
restaurant1 = Restaurant()
# creating instance variable
restaurant1.name = '7 Eleven'
restaurant1.owner = 'Rahim'
# calling instance method will replacee self argument by
# instance object here restaurant1
restaurant1.details()
restaurant1.details_with_address('Bogra')
# checking object type
print ( type(restaurant1) )

Viewing all articles
Browse latest Browse all 9596

Trending Articles