How to detect an undefined method?

Kirill Ratkin kirill.ratkin at devoteam.com
Sun Mar 27 12:57:38 EDT 2022


Hi

You can get all methods of your object and check the method you want to 
call is there or not.

|methods = [method for method in dir(<your_object>) if 
callable(getattr(<your_object>, method))] if 'method_you_need' in 
methods: <your_object>.<method_you_need> // BR |

27.03.2022 12:24, Manfred Lotz пишет:
> Let's say I have a Python app and have used an undefined method somewhere. Let
> us further assume I have not detected it thru my tests.
>
> Is there a way to detect it before deploying the app? pylint doesn't notice it.
>
>
> Minimal example:
>
> #!/usr/bin/env python3
>
> import logging
> from logging import Logger
> from random import randrange
>
> def main():
>      """
>      Below logger.err gives
>
>      'Logger' object has no attribute 'err'
>      """
>
>      logger = logging.getLogger('sample')
>      logger.setLevel(logging.DEBUG)
>      handler = logging.StreamHandler()
>      logger.addHandler(handler)
>
>      num = randrange(0,1000)
>      if num == 0:
>          logger.err("got zero")
>      else:
>          logger.info(f'got a positive integer: {num}')
>
> if __name__ == "__main__":
>      main()
>
>
>


More information about the Python-list mailing list