Get the source of a class reliably?!?

computermaster360 . computermaster360 at gmail.com
Mon Apr 29 16:27:30 EDT 2019


Does anyone have an idea why classes don't contain their definition
line number as functions or methods do?

>>> some_fun.__code__.co_firstlineno
123
>>> SomeClass.???

This leads to some funny stuff when using `inspect`, such as this:

-- weird.py -----------------------------
"""
class C:
  HAHAHA! YOU FOOL!
"""

class C:
  "this is a perfectly ok class"

  class C:
    "this class is nice"
-----------------------------------------

>>> inspect.getsource(weird.C)
class C:
  HAHAHA! YOU FOOL!


Why ???


More information about the Python-list mailing list