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
  • 39,665
  • 11
  • 104
  • 149
Nooa
  • 21
  • 1
  • 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. – Nooa 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
  • 21,584
  • 4
  • 41
  • 73
Evan Snapp
  • 523
  • 5
  • 21
  • Cannot do that. I can change only the soonImported class (and the file where it is). It is a homework. – Nooa Oct 03 '18 at 16:07
  • @mrSimple If you can only change the class, why do you need to import it? – Olivier Melançon Oct 03 '18 at 16:08
  • @mrSimple It literally isn't possible to import a method. Either you misunderstood the homework assignment, or the homework assignment is impossible. – Aran-Fey Oct 03 '18 at 16:11
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,415
  • 5
  • 18
  • 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,860
  • 1
  • 17
  • 31
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,590
  • 1
  • 8
  • 20