0

In Python, a module can have functions, classes or both in the same module. When I am importing module into another module, is there a way to determine if the module has just functions or classes or both?

The reason I ask this is because classes will have to instantiated as objects whereas functions don't. So everytime I import, I want to know what I dealing with without getting into opening the module esp. the external modules.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    When you work with 3rd-party modules, you need either to find docs or read the code. – PM 77-1 Oct 28 '21 at 19:36
  • 1
    If the code follows PEP8, classes are named in `CamelCase`, and functions are named in `snake_case`. But on a more general perspective, you shouldn't have to worry about this. From Python's point of view, classes and functions are things that you can call, which may or may not accept some data and may or may not return some other data. In fact, sometimes package maintainers write functions that look and work like classes, and vice-versa. You'll often figure it out by reading the docs, which you'll want to do before importing anyway. – jfaccioni Oct 28 '21 at 19:42
  • 1
    Only modules (.py files) can be imported. They may *contain* function and/or classes and you will have to read the documentation or source code to know that. Many modules in the standard library have a docstring at the beginning, so from the Python console you can type `>>> import module_name` then `>>> help(module_name)` to see it. – martineau Oct 28 '21 at 20:53
  • Thanks @jfaccioni – user17273882 Oct 29 '21 at 16:13
  • Thanks @PM77-1. – user17273882 Oct 29 '21 at 16:13
  • thanks @martineau. – user17273882 Oct 29 '21 at 16:13

1 Answers1

0

This may be what you are looking for: How to check whether a variable is a class or not?

Not really sure what your use case is, but you may also find a use for the built-in dir function.

BStarcheus
  • 23
  • 6
  • A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – martineau Oct 28 '21 at 20:48