Skip to content

Instantly share code, notes, and snippets.

@juan-reynoso
Created October 13, 2021 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan-reynoso/883df73e81d2274eb39d05ea88f05c05 to your computer and use it in GitHub Desktop.
Save juan-reynoso/883df73e81d2274eb39d05ea88f05c05 to your computer and use it in GitHub Desktop.
Python Object-Oriented programming example.
# Python Object-Oriented programming
# Define a class called Dog
# Python class names are written in CapitalizedWords notation by convention.
class Dog:
# define a method called __init__ it is a special init method.
# "__init__" is a reseved method in python classes. It is known as a constructor in object oriented concepts.
# This method called when an object is created from the class and it allow the class to initialize the attributes of a class.
def __init__ (self, name, age):
# self represents the instance of the class. By using the "self" keyword. We can access the attibutes and methods of the
# class in python.
# define name and age as attributes.
# Creates an attribute called name and assigns to it the value of the name parameter.
# attribute parameter
# | |
# v V
self.name = name
# Creates an attribute called age and assigns to it the value of the age parameter.
# attribute parameter
# | |
# v V
self.age = age
# instance method is a function
# define the method called say_name
def say_name(self):
# display name
print("Hello, my name is " + self.name ,".")
# define the method called say_age
def say_age(self):
# display age
print ("Hello, I am " ,self.age ,"years old.")
# create a instance
rocky = Dog("Rocky",5);
# call methods
rocky.say_name()
rocky.say_age()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment