Questions tagged [callable-object]

A callable object is an object which also can act as a function. Some languages allow arrays, hash tables or strings to be functions.

67 questions
766
votes
17 answers

What is the difference between __init__ and __call__?

I want to know the difference between __init__ and __call__ methods. For example: class test: def __init__(self): self.a = 10 def __call__(self): b = 20
sam
  • 18,509
  • 24
  • 83
  • 116
29
votes
7 answers

How to check if template argument is a callable with a given signature

Basically, what I want to achieve is compile-time verification (with possibly nice error message) that registered callable (either a function, a lambda, a struct with call operator) has correct signature. Example (contents of the static_assert are…
pzelasko
  • 2,082
  • 1
  • 16
  • 24
12
votes
1 answer

No matching function error when passing lambda function as argument

I have a list of numbers. I am trying to filter the list and only keep the positive numbers. I am trying to do it by passing a lambda as an argument. I wonder why I get function mismatch error. #include #include #include…
ar2015
  • 5,558
  • 8
  • 53
  • 110
9
votes
2 answers

How to determine if Julia object is callable

In Julia, what's the best way to determine whether an object is callable? (E.g. is there an analog of python's callable function?) EDIT: Here's what one could wish for: f() = println("Hi") x = [1,2,3] a = 'A' callable(f) # => true callable(x) …
Yly
  • 2,150
  • 4
  • 20
  • 33
8
votes
2 answers

Python how to type hint a Callable with __wrapped__

When passing around functions, I normally type hint them with typing.Callable. The docs for collections.abc.Callable state that it has four dunder methods: class collections.abc.Callable ABCs for classes that provide respectively the methods…
6
votes
1 answer

How to document callable classes annotated with @dataclass in sphinx?

I researched this topic and cannot see a clear solution. There is a similar SO question My problem is that I have a class with attr.dataclass and typing_extensions.final annotations and I don't want them to be documented but I still want to describe…
Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82
6
votes
2 answers

Getting arguments for any callable object?

I am interested in obtaining a general way to obtain a list of arguments and keyword arguments taken by a callable Python object. This is straight-forward for functions with the inspect.getargspec function, for example: import inspect from functools…
6
votes
1 answer

C++ this and constant object

could you tell me why this code works? There is overloaded operator() which is used by replace_if algorithm. In main function I've created constant object of IsEqual class so only constant function member should be used. Somehow constancy isn't…
Joker
  • 63
  • 5
5
votes
7 answers

On Raspbian run multiple python Versions simultaniously

I have some old programs from back in the times when python 3.1 came out. In the program I often used the Callable() to pass a function and it's parameters like this to my TKinter application: tvf.mi(datei_bu, text=datei_opt,…
monamona
  • 1,195
  • 2
  • 17
  • 43
5
votes
2 answers

What is the definition of a callable type?

Background: I am trying to understand why static and class methods are not callable while being descriptors, while ordinary methods of a class (i.e. methods of a class which are neither static or class methods) and functions which are not attributes…
Tim
  • 1
  • 141
  • 372
  • 590
4
votes
2 answers

Functional composition with std::bind

I am reading the second edition of the beautiful book of Nicolai Josuttis on C++11 STL. I found the following piece of code: #include #include int main() { auto plus10 = std::bind(std::plus(), …
fabiop
  • 179
  • 10
4
votes
1 answer

Pyspark string pattern from columns values and regexp expression

Hi I have dataframe with 2 columns : +----------------------------------------+----------+ | Text | Key_word | +----------------------------------------+----------+ | First random text tree cheese cat |…
Anna
  • 83
  • 1
  • 1
  • 5
4
votes
2 answers

Function call and the __call__ attribute

Python's callables are objects that have a __call__ method. They are most of the time functions, but might be class instances as well. But it so happens that functions do have a __call__ method. Therefore, a __call__ method has a __call__ method as…
Right leg
  • 16,080
  • 7
  • 48
  • 81
4
votes
3 answers

Get PHP callable arguments as an array?

Say I have a callable stored as a variable: $callable = function($foo = 'bar', $baz = ...) { return...; } How would I get 'bar'? if (is_callable($callable)) { return func_get_args(); } Unfortunately func_get_args() is for the current function,…
Helen Che
  • 1,951
  • 5
  • 29
  • 41
3
votes
3 answers

How to use a callable as a filter in logging python

I don't know how to use this new property of python3.2. There, instead of implementing logging.Filter class, one can use a callable. I'm trying to use dictConfig for my logger (in python). In that, I want to add a filter such that it will pass if…
user44875
  • 356
  • 1
  • 7
1
2 3 4 5