asked    Lester     2018-10-22       python       38 view        1 Answer

[SOLVED] How to display definition of a class in Python

I was going through this question : How do I return the definition of a class in python?

But I am unable to display the class definition. I am getting the below error:

>>> class A:
...     pass
>>> import inspect
>>> source_text = inspect.getsource(A)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\**\Python\Python36\lib\", line 968, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Users\**\Python\Python36\lib\", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Users\**\Python\Python36\lib\", line 768, in findsource
    file = getsourcefile(object)
  File "C:\Users\**\Python\Python36\lib\", line 684, in getsourcefile
    filename = getfile(object)
  File "C:\Users\**\Python\Python36\lib\", line 654, in getfile
    raise TypeError('{!r} is a built-in class'.format(object))
TypeError: <module '__main__' (<_frozen_importlib_external.SourceFileLoader object at 0x0000026A79293F60>)> is a built-in class

Can someone please advise what am I doing wrong here? Thanks.

  1 Answer  

        answered    Payne     2018-10-22      

The inspect.getsource() function only works if there is a text file available to load the source code.

You typed the definition of the class into the interactive interpreter, which doesn't keep the original source around when compiling that source into in-memory class and code objects.

Put your class definition into a module, import the module, and then use inspect.getsource().

inspect.getsource() works by first finding the module for a given object (for classes, by looking at the ClassObj.__module__ attribute for the module name, then getting the module via sys.modules[modulename]) then seeing if the module has a __file__ attribute from which a readable source file can be determined. If there is such a filename and it can be read, then the inspect module reads that file to then search for the class ClassName: line and giving you all lines from that point on with same or deeper indentation. The interactive interpreter executes everything in the __main__ module and there is no __file__ attribute for the interpreter, so any attempts at loading source code for objects defined there will simply fail.

If you just wanted to know what members the class defines, use dir() or help() on the object instead. You don't need to see the full source code for that information.

Your Answer

 2018-10-22         Phoenix

Public types cant be declared in class modules

I want to create a function in a VBA (access) class module which returns a result type. Like this:Public Type CheckResult isOk As Boolean errorText As StringEnd TypePublic Function Check() As CheckResult ...end functionThis function is used in a lot of different places. But the compiler tells me, that it is not allowed to declare public types in class modules? Why is this? Where to put my Type instead? Should I create a new module just for this type? Or is it better to create a "AllTypes"-Module to put all Type-declarations in? As for the Why question:Class modules ...
 vba                     2 answers                     17 view
 2018-10-22         Joanne

Python iterate through object to create tables with Flask-SQLAlchemy

EDITEDI've been trying to create an class that will allow me to iterate through a list, or dictionary, and generate tables and rows.The code starts as follows:from flask_sqlalchemy import SQLAlchemy as safrom flask import Flask as flimport pymysqlpymysql.install_as_MySQLdb()app = fl(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:the_other_stuff'app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] =FalseDEBUG =Truedb = sa(app)a=['some_stuff','gaaahhhh','lalala','rawr','pizza']class test(db.Model): __tablename__ = 'stuff' id = db.Column('id', db.Integer, primary...
 python                     2 answers                     26 view
 2018-10-22         Louis

JavaScript - Accessing a child variable in a parent class

In JavasScript, is it possible to define a variable in a child class, then access it in a parent class? I imagine the child class would look something like this:export default class ChildClass extends ParentClass { constructor() { this.path = 'register'; }}A use-case scenario is an HTTP service class as the parent that contains general, reusable methods and a child class that defines the exact route that will be accessed. Assuming it's possible, how could I then access this.path in the parent class? You don't need to do anything special, it just works.As men...
 javascript                     1 answers                     21 view