0

Here's a file named "importable" where I try to import the method test function.

class SoonImported:
   def __init__(self, x):
       self.x = x

   def methodTest(self):
       print(self.x)

In another file I have this line of code that cannot be changed (because my example is based on a homework).

from importable import SoonImported, methodTest

When I try to run this script it gives me an Import error and says that it "cannot import name 'methodtest'". How should I correct the soonImported class in order to make it work?

Thanks for your help.

Aran-Fey
  • 35,525
  • 9
  • 94
  • 135
mrSimple
  • 21
  • 3
  • 4
    Why would even want to import a method? You have to create a `SoonImported` instance before you can use the method anyway. Importing a method wouldn't achieve anything useful. – Aran-Fey Oct 03 '18 at 16:03
  • What are you trying to do? Methods are properties of the class and of the class instances. If you import the class, the methods are included. – Håken Lid Oct 03 '18 at 16:03
  • How is methodTest called in the file you aren't allowed to edit? I think this will help us know how it should be defined. Is it called as methodTest() or methodTest(something)? The self object is passed automatically to a method inside a class, but not for a general function definition. – Andrew McDowell Oct 03 '18 at 16:13
  • methodTest is just called in a file that I cannot edit. We write the code and then copy paste it to a page where it is tested. That's just how it is and I can do nothing to change it. But methodTest is called in original assignment methodTest(stringname). But that happens only after it is imported. – mrSimple Oct 03 '18 at 16:19

4 Answers4

3

It looks like methodTest is defined inside of SoonImported. So it would be addressed like this from importable import SoonImported and then methodTest would be called like

instance = SoonImported()
instance.methodTest()
Olivier Melançon
  • 20,340
  • 3
  • 37
  • 69
Evan Snapp
  • 463
  • 3
  • 18
0

You can't import a method from a class without the rest of the class because the method only exists within the context of the class. Think of importing like borrowing. You want to borrow a lawn mower from your friend. Except that what you have there is like trying to borrow a lawn mower from your friend without making friends with anybody first. If you don't have any friends, how can you expect to borrow lawn mowers from anybody?

mypetlion
  • 2,348
  • 5
  • 16
  • 22
0

Assuming your homework is correctly written, it seems likely that the methodTest should be a function rather than a method of the SoonImported class. I'm guessing in the second file you have lines somewhere of something similar to:

SoonImported_object = SoonImported("Some Text")
methodTest(SoonImported_object)

If this is the case, then methodTest is not a method, but a function and should be defined as something like the following;

class SoonImported:
    def __init__(self, x):
        self.x = x

def methodTest(SoonImported_object):
    print(SoonImported_object.x)
Andrew McDowell
  • 2,710
  • 1
  • 14
  • 28
0

Just import the class and use it like your normally would

importable.py

class SoonImported:
    def __init__(self, x):
        self.x = x

    def method_test(self):
        print(self.x)

vash.py

from importable import SoonImported

a = SoonImported(1)
a.method_test()
python3.7 vash.py
1
vash_the_stampede
  • 4,449
  • 1
  • 6
  • 20