'module' object is not callable - calling method in another file

前端 未结 2 487
北荒
北荒 2020-12-28 11:55

I have a fair background in java, trying to learn python. I\'m running into a problem understanding how to access methods from other classes when they\'re in different file

2条回答
  •  死守一世寂寞
    2020-12-28 12:39

    • from a directory_of_modules, you can import a specific_module.py
    • this specific_module.py, can contain a Class with some_methods() or just functions()
    • from a specific_module.py, you can instantiate a Class or call functions()
    • from this Class, you can execute some_method()

    Example:

    #!/usr/bin/python3
    from directory_of_modules import specific_module
    instance = specific_module.DbConnect("username","password")
    instance.login()
    

    Excerpts from PEP 8 - Style Guide for Python Code:

    Modules should have short and all-lowercase names.

    Notice: Underscores can be used in the module name if it improves readability.

    A Python module is simply a source file(*.py), which can expose:

    • Class: names using the "CapWords" convention.

    • Function: names in lowercase, words separated by underscores.

    • Global Variables: the conventions are about the same as those for Functions.

提交回复
热议问题