Can we create class in function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we create class in function?

Function, class

5th Apr 2024, 11:53 AM
Bhagwat Manwatkar
Bhagwat Manwatkar - avatar
2 Answers
+ 4
Usually no, it is not possible to create classes inside functions, but it may also depend on the programming language, which you did not specify. "confuse" is not a language, please use correct tags.
5th Apr 2024, 11:56 AM
Tibor Santa
Tibor Santa - avatar
0
Bhagwat Manwatkar , Please add a tag for the language you're asking about, or give more info. It's possible in Python. I just verified it, but I haven't explored it. def f(): class C: def __init__(self, a): self.a = a return C x = f()(10) print(x.a) # 10
6th Apr 2024, 5:51 AM
Rain
Rain - avatar