5

i want to import some function in a class without needing to import the whole class.

I have some class as follows


MyClassFile.py

class MyClass:
        def __init__(self):
            pass #Some init stuff

        def some_funcs(self):
            pass #Some other  funcs

        @staticmethod
        def desired_func():
            pass #The func i want to import

MyScript.py

from MyClassFile import MyClass.desired_func

or

from MyClassFile.MyClass import desired_func

I tried to import that way but isn't work, is there any way to do it?

David Z
  • 128,184
  • 27
  • 255
  • 279
Ivan Gonzalez
  • 446
  • 1
  • 5
  • 14
  • Hi Ivan, what's your reason for not wanting to import the class? – David Z Jan 09 '18 at 23:14
  • 4
    There is no way to do it in one import statement. When would that be useful? If it's a static method, make it a standalone function. – vaultah Jan 09 '18 at 23:14
  • 1
    @Miket25 That doesn't seem to be asking the same question. This is asking about using import to access attributes of a class, which is not a factor in the question you linked. – David Z Jan 09 '18 at 23:19
  • @DavidZ The question asks how do you import some function of a class and have its use available in another file. The question I linked with the accepted answer points to that it's not possible to link attributes of a class, being the methods. Can you please explain what's different; I'm not sure if I get your difference? – Miket25 Jan 09 '18 at 23:23
  • @Ajax123 why did you reopen this question? – vaultah Jan 09 '18 at 23:27
  • It is useful because the function i want to import is a parser used in another script, if i had put the function outside the class it wont be categorized well because there are multiple classes with multiple parsers (i wont name the function "parser_for_class_A" and so on, instead ClassA.parser) and i dont want to import the whole class because i think is not Pythonic to import stuff that i wont use it – Ivan Gonzalez Jan 09 '18 at 23:27
  • @vaultah while the question can be reclosed, the answer provided in https://stackoverflow.com/questions/17346950/how-to-import-a-single-function-to-my-main-py-in-python-from-another-module requires the use of `sys`, thus requiring an additional import, which the OP seems to want to avoid. – Ajax1234 Jan 09 '18 at 23:30
  • @Ajax1234 there's no indication that Ivan wants to avoid importing the `sys` module. On second thought, that was a poor duplicate target, so no harm was done, I guess – vaultah Jan 09 '18 at 23:32
  • Import sys to import a function? Thats sound awful isnt' it? – Ivan Gonzalez Jan 09 '18 at 23:34
  • @IvanGonzalez you **always** import the whole module. You can't import just part of a module. The whole module is executed. If you do something like `from my_module import my_function` you can still check `sys.modules` and find `my_module` even if `my_module` isn't in the global namespace. – juanpa.arrivillaga Jan 09 '18 at 23:34
  • Furthermore the answers [there](https://stackoverflow.com/questions/17346950/how-to-import-a-single-function-to-my-main-py-in-python-from-another-module) are both garbage. New dupe is better, though. – wim Jan 09 '18 at 23:35
  • @vaultah the duplicate mentioned now is much more specific to the question. Thank you for finding it. – Ajax1234 Jan 09 '18 at 23:36
  • @Miket25 I suppose it doesn't matter now, but if you'd still like me to explain, I'd be happy to do so in a chat room. – David Z Jan 09 '18 at 23:45
  • Ivan, the etiquette here is _not_ to edit the answer into your question, so I've rolled back your edit accordingly. If you want to mark a particular answer as having solved your problem, all you should do is accept it (and probably upvote it too), which you've already done. So you're all set. – David Z Jan 09 '18 at 23:59

1 Answers1

8

To play devil's advocate:

# MyScript.py
desired_func = __import__('MyClassFile').MyClass.desired_func

On a more serious note, it sounds like your staticmethod should actually just be a module level function. Python is not Java.

If you insist on having a staticmethod, pick one of the options below:

  1. Import the class as usual and then bind a local function: f = MyClass.desired_func
  2. In MyClassFile.py, after the class definition block, alias a module-level function to the staticmethod and then import that name directly.
wim
  • 338,267
  • 99
  • 616
  • 750