creating objects in python code example
Example 1: how to make a class in python
class Person:
def __init__(self, _name, _age):
self.name = _name
self.age = _age
def sayHi(self):
print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
p1 = Person('Bob', 25)
p1.sayHi()
Example 2: class and object in python
class PartyAnimal:
def Party():
an=PartyAnimal()
an.Party()
Example 3: how to create an object in python
class ClassName:
self.attribute_1 = variable_1
self.attrubute_2 = variable_2
def __init__(self, attribute_3, attribute_4):
self.attribute_3 = attribute_3
self.attribute_4 = attribute_4
def method(self):
print("This is a method example.")
object = Object(4, "string")
object.method()
Example 4: How to make a new class in python
class Fruits():
def __init__(self, name, colour, taste):
self.name = name
self.colour = colour
self.taste = taste
fruit1 = Fruits(apple, red, sweet)
print(fruit1.name)
print(fruit1.colour)
print(fruit1.taste)