Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

difference between object and class in python

# In Object Oriented Programming (OOP):
# An object is an instance of a class.
# A class is a blueprint/template for an object.

# A class is a collection of 
#     - attributes (data/variables) and 
#     - methods (functions) 
# which fulfill a specific function.

# Example: 

class Car():
    def __init__(self, brand, model, colour, owner):
        self.brand = brand
        self.model = model
        self.colour = colour
        self.owner = owner
            
    def honk(self):
        print(f"I am {self.owner}'s {self.colour} {self.brand} {self.model}!")
        
bob_car = Car('Volvo', 'Sedan', 'black', 'Bob') 
amy_car = Car('Mini', 'Cooper', 'red', 'Amy')

bob_car.honk()
amy_car.honk()

# >>> I am Bob's black Volvo Sedan!
# >>> I am Amy's red Mini Cooper!

# Car is the blueprint for the objects bob_car and amy_car.
# bob_car and amy_car objects were instantiated from the class Car.
Comment

PREVIOUS NEXT
Code Example
Python :: pyspark groupby multiple columns 
Python :: django logout user 
Python :: python create list from range 
Python :: adf test python 
Python :: how to get current date and time in python 
Python :: pandas profile 
Python :: round up division python 
Python :: python reverse words in string 
Python :: random picker in python 
Python :: python capture desktop as video source 
Python :: python Pyramid Patterns 
Python :: numpy get variance of array 
Python :: python ssl module is not available 
Python :: int to string python 
Python :: python adding digits 
Python :: pandas read from txt separtion 
Python :: create new list in for loop python 
Python :: random question generator python 
Python :: python function as parameter 
Python :: change x axis frequency 
Python :: how to delete role discord py rewrite 
Python :: how to get a number from a string in python 
Python :: _getfullpathname: path should be string, bytes or os.PathLike, not list 
Python :: how to take input complex number in python 
Python :: pyspark show all values 
Python :: how to convert cost to float in python 
Python :: commentaire python 
Python :: spacy config 
Python :: spawn shell using python 
Python :: how to calculate the sum of a list in python 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =