Post

PythonPython

A
Anthony A

Posted on 20th April 2024|16 views

0
votes

Import Class Python

How can I import a class from another file in python?

Answers
P
christina joce

Posted on 20th April 2024

Assume there is your file called MY_file which contains a class called mindmajix something like this 

class mindmajix:

def __init__(self,val):

self.val=val

def getVal(self):

return self.val

Now to import a class called mindmajix to your program you can do this

sys.path.append(".")

from my_file import mindmajix

newClass = myClass(10)

val = newClass.getVal()

Output:

10

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos