Does anybody have a good example of a Functor in Python?
bumsfeld
413
Nearly a Posting Virtuoso
Recommended Answers
Jump to PostWhen you say "functor" are you using the term in the C++ sense (i.e. a callable object) or something else?
And would a good example be the most simple one that shows you how to create one or something that shows you where they'd be useful?
Jump to PostHere is a typical example:
class Accumulator(object): ''' remembers self.n __call__() allows the class instance to be called as a function ''' def __init__(self, n=0): self.n = n def __call__(self, x): self.n += x return self.n # create an instance and initialize data = 4 acc = …
All 6 Replies
sepp2k
378
Practically a Master Poster
Lardmeister
461
Posting Virtuoso
Gribouillis
1,391
Programming Explorer
Team Colleague
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
bumsfeld
413
Nearly a Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.