how to call a function inside a class in python code example
Example 1: python call function in class
class Student:
def __init__(self):
self.name = None
def set_name(self, word):
self.name = word
return self.get_name()
def get_name(self):
return self.name
a = Student()
print(a.set_name("Hello"))
Example 2: call a function from another class python
class A:
def method1(arg1, arg2):
class B:
A.method1(1,2)
Example 3: how to create a class inside a function in python
def create_class():
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
return Person
my_class = create_class()
person1 = my_class("John", 24)
print(f'{person1.name} is {person1.age} years old.')