Home > Mobile >  hi i want to make a class in python then use the class in another python file in python
hi i want to make a class in python then use the class in another python file in python

Time:08-27

i want to make a class in python then use the class in another python file in python like we have a class called types then types is stored in class.ipynb then i want to make another python file the use import to import types but when i want to use types it give an error

note: i am using jupyter notebook

error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [19], in <cell line: 1>()
----> 1 from types import Square
      3 newClass = Square(5)
      4 val = newClass.getVal()

ImportError: cannot import name 'Square' from 'types' (C:\Users\DAVID\anaconda3\lib\types.py)
 

code:

types

class Square:
    def __init__(self,val):
        self.val=val
    def getVal(self):
        return self.val*self.val

class2

from types import Square
 
newClass = Square(5)
val = newClass.getVal()
 
print(val)

note: class2 is giving a error

Thank you

  • Related