1

this is my attempt in python 3.4:

import imp

def my_example_function():
  print("my_example_function:")

class my_class:
  def __init__(self):
    module = imp.load_source(my_example_function, __file__)

my_class_instance = my_class()

result is:

Traceback (most recent call last):
  File "D:\Python34\test.py", line 10, in <module>
    my_class_instance = my_class()
  File "D:\Python34\test.py", line 8, in __init__
    module = imp.load_source(my_example_function, __file__)
  File "D:\python34\lib\imp.py", line 166, in load_source
    spec = util.spec_from_file_location(name, pathname, loader=loader)
  File "<frozen importlib._bootstrap>", line 932, in spec_from_file_location
  File "<frozen importlib._bootstrap>", line 1439, in is_package
AttributeError: 'function' object has no attribute 'rpartition'

It looks like some kind of internal error a bug in python itself.

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • Importing is for modules, not functions.. – Martijn Pieters May 11 '14 at 10:12
  • I don't understand much from http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object as for now, can someone tell me whats wrong with my code ? Is thing that I there want to attempt even legal in python ? – rsk82 May 11 '14 at 10:20
  • As I said; importing is for modules. Adding a method to an instance is not importing. – Martijn Pieters May 11 '14 at 10:24
  • But anyway this error is a bug of itself, its very cryptic. – rsk82 May 11 '14 at 10:29
  • No, the function you are using expects a string; it is documented as such. Passing in anything else breaks. – Martijn Pieters May 11 '14 at 10:40
  • Ok, thanks, by the time I noticed it myself. The thing is the breaking message is not straitforward as it should be. Error sholud be: "function such and such expected string but .... given". – rsk82 May 11 '14 at 10:43

1 Answers1

2

the error says, that a 'function'-object has no attribute 'rpartition'. The only function object, you give to load_source is my_example_function but a name aka string is expected. And strings have a method called rpartition. So if you use load_source correctly, you won't get an error. But I cannot say, how, because I don't understand what you try to do.

Daniel
  • 42,087
  • 4
  • 55
  • 81