1

The init function inside a Class is annotated the following way:

    def __init__(self, directory: str, transforms: Callable = None, extension: str = '.jpg'):

The question is what Callable = None is referring to.

Conventionally if the transforms - argument annotation would mean to intake a Callable (i.e. a function) then the input parameters would need to be defined as well as the output, as an example it could be: transforms: Callable[[int,int], int] where the [int,int] would be the function parameters as input, and the latter int` would be the return. But here this is not the case.

What does the Callableannotation expect as input and return in this case?

2 Answers 2

0

According to Python Documentation

A plain Callable is equivalent to Callable[..., Any], and in turn to collections.abc.Callable.

3
  • So Callable = None is regarded as "plain", and hence input and output are freely choosable?
    – Alex
    Apr 5, 2022 at 1:03
  • @Alex right, Callable alone just means some function needs to be filled here, but no extra information is provided.
    – whilrun
    Apr 5, 2022 at 1:04
  • That really makes sense, thank you! I feel the way the class is written is unusual, while typing is supposed to facilitate the task at hand.
    – Alex
    Apr 5, 2022 at 1:13
0

It's very hard to tell what this function would take as input and output without any information about the class, or what the class does. It completely depends on what the class does, and you should probably read the documentation of the class.
From looking at the default value of extension, the class expects .jpg files. If it is a graphics or image manipulation library like pillow, the callable may need to take image data of some kind.
As I said, however, it's impossible to tell without more information.

2
  • Thanks for the answer, this is a machine learning task that is supposed to create a costum dataset similar to this pytorch template, the class also inherits from torch.utils.data.dataset.Dataset, and the Callable in question is an image transformation, pretty much: ` def get_transforms(size: Tuple[int, int]) -> Callable: """ Transforms to apply to the image.""" transforms = [ToTensor()].append(transforms.Resize(size)) return Compose(transforms) `
    – Alex
    Apr 5, 2022 at 1:07
  • Thanks for the answer, think its solved below.
    – Alex
    Apr 5, 2022 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.