python define object 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: 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 3: how to use class's in python
class person:
name = "jake"
age = 13
x = vars(person)
print(x)
Example 4: how to define a class in python
class a_class:
def __init__(self, input1):
self.__input1 = input1
def return_input(self):
return self.__input1
a_class_object = a_class("input string")
print(a_class_object.return_input())