-2

this is the full code:

class Solution:
    def equalFrequency(self, word: str) -> bool:
        

I want to know what is word:str -> bool: means in python

Can anyone explain this with examples. I only want to know what is word: str -> bool: means in python.

  • Those are annotations that tell things like linters what should be passed to a function and what it will return. They may also help people figure out how to use a function. Because nobody can write a doc string I guess?? They don't have any affect on the code itself. https://docs.python.org/3/howto/annotations.html – tdelaney Nov 05 '22 at 02:55
  • See better canonical Q&A: [What are type hints in Python 3.5?](https://stackoverflow.com/q/32557920/2745495) – Gino Mempin Nov 05 '22 at 02:58
  • You are actually asking about 2 separate things: the type hint for `word: str` and the type hint for the return value `-> bool`. It's not one `word:str -> bool`. But they both fall under the search term "type hints" or "type annotations", which the links above already explain. – Gino Mempin Nov 05 '22 at 03:01

1 Answers1

0

This is python typing.

word: str says that the argument word is expected to be of type str (string)

def equalFrequency(self, word: str) -> bool says that function equalFrequency is expected to return a bool (boolean), so we should get a true or false values from the function

Examples:

equalFrequency('hello') ==> Correct

equalFrequency(123) ==> Incorrect

Correct:

class Solution:
    def equalFrequency(self, word: str) -> bool:
        return true

Incorrect:

class Solution:
    def equalFrequency(self, word: str) -> bool:
        return 123

Virt
  • 13
  • 4
  • 1
    But to be clear, python is dynamic and doesn't have typing. They are type hints for people and linters. – tdelaney Nov 05 '22 at 02:56
  • 1
    `equalFrequency(123) ==> Incorrect` : Unless you are using a linter like mypy, Python will not actually raise an error on that code, and will happily go along and assign the int 123 to `word`. Same as for the return value. – Gino Mempin Nov 05 '22 at 03:16