xxxxxxxxxx
class IntellipaatClass:
a = 5
def function1(self):
print(‘Welcome to Intellipaat’)
#accessing attributes using the class object of same name
IntellipaatClass.function(1)
print(IntellipaatClass.a)
xxxxxxxxxx
class Mammal:
def __init__(self, name):
self.name = name
def walk(self):
print(self.name + " is going for a walk")
class Dog(Mammal):
def bark(self):
print("bark!")
class Cat(Mammal):
def meow(self):
print("meow!")
dog1 = Dog("Spot")
dog1.walk()
dog1.bark()
cat1 = Cat("Juniper")
cat1.walk()
cat1.meow()
xxxxxxxxxx
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
xxxxxxxxxx
class ClassName(object): #"(object)" isn't mandatory unless this class inherit from another
def __init__(self, var1=0, var2):
#the name of the construct must be "__init__" or it won't work
#the arguments "self" is mandatory but you can add more if you want
self.age = var1
self.name = var2
#the construct will be execute when you declare an instance of this class
def otherFunction(self):
#the other one work like any basic fonction but in every methods,
#the first argument (here "self") return to the class in which you are
xxxxxxxxxx
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_name(self):
print(f"Hello, I am {self.name}")
p1 = Person("Sara", 18)
p1.say_name()
xxxxxxxxxx
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the samne
thing. In addition variables that are stored inside a class are named
attributes. The point of a class is to call the class later allowing you
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
xxxxxxxxxx
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
xxxxxxxxxx
class awwab(object):
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print("Hello, my name is",self.name,"and I am",self.age,"years old!")
awwabasad = awwab("Awwab Asad", 11)
print(awwabasad.speak())
xxxxxxxxxx
class Greet:
def __init__(self, names):
self.names = names
def say_hello(self):
for name in self.names:
print("Hello " + name)
xxxxxxxxxx
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def greet(self, person_to_greet):
# person_to_greet will be another Person object
print(f"Hey {person_to_greet.name}, nice to meet you I'm {self.name}")
def ask_age(self, ask_from):
print(f"{self.name}: {ask_from.name}, How old are you?")
print(f"{ask_from.name}: i am {ask_from.age}")
# Creating a person object
tom = Person("Tom", 50, "Male")
# we can also create an object with keyword arguments
jack = Person(name="Jack", age=19, gender="Male")
# Here we call the greet method of tom, and we pass the Jack Person Object Created above
tom.greet(jack)
# if we call the greet method of jack and pass the Tom person object, then jack greets tom
jack.greet(tom)
# Here Jack will ask age of tom, and tom will reply with his age
jack.ask_age(tom)