0
def is_acceptable_password(password: str) -> bool:
    cond1 = len(password) > 6
    cond2 = any(map(str.isdigit, password))
    if cond1 & cond2 == True:
        return True
    else:
        return False

In this code I do not understands how "str.isdigit" works. In my logic I need to write it like "password.isdigit", because I want to check whether the digit is in it, but only "str.isdigit" works.

  • In this case you should look into the docs of ``map`` – Mike Scotty Jan 03 '23 at 12:29
  • `any(char.isdigit() for char in password)` – Tom McLean Jan 03 '23 at 12:30
  • 1
    @Tomerikoo, I am not sure if OP confusion is on `map()` or caused by the fact they don't undrestand how `str.isdigit()` would work – buran Jan 03 '23 at 12:31
  • also ``return cond1 and cond2`` instead of the last 4 lines – Mike Scotty Jan 03 '23 at 12:31
  • 2
    An instance method can either be called as `instance.method(args)` or `class.method(instance, args)`. By passing `str.isdigit` to `map` it calls `str.isdigit(c)` for each `c` in `password`. As mentioned, an equivalent code which makes it easier to understand is `cond2 = any(str.isdigit(c) for c in password))` (which is more reasonably written as `any(c.isdigit() for c in password)`) – Tomerikoo Jan 03 '23 at 12:33
  • the `map` function will iterate over each char in the string "*password*" and execute the function `str.isdigit` for each char. the `any` function will check if at least one condition is true to return true. in this case, if any char in the string is a digit, then will return true. – Paulo Pereira Jan 03 '23 at 12:35
  • 1
    [Calling instance method using class definition in Python](https://stackoverflow.com/questions/51505666/calling-instance-method-using-class-definition-in-python) – matszwecja Jan 03 '23 at 12:40
  • 1
    Thanks @matszwecja I couldn't find that. Feel free to ping me next time so I can update the dupe list (I happened to have the page still open this time) – Tomerikoo Jan 03 '23 at 13:11

0 Answers0