0

I was looking at some python2 code that looks something like this:

def foo(self):
    pass

foo.boolean = 'WTH'

and this code seems to be syntactically correct. But what purpose does it serve?

Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
  • Of course it is *syntactically* correct, `foo.bar = baz` is correct. Anyway, function objects are like any other object, you can assign attributes to them if you'd like, just like `class Foo: pass; foo = Foo(); foo.boolean = "WTH"`, however, I can't think of a lot of non-hacky use-cases off the top of my head – juanpa.arrivillaga Jan 06 '20 at 22:30
  • `function` is one of the few (only?) builtin types that accept arbitrary attributes, though. – chepner Jan 06 '20 at 22:57
  • @chepner definitely not only, consider exceptions, for example. Built-in containers definitely, though, I think. – juanpa.arrivillaga Jan 06 '20 at 23:09

0 Answers0