3

I'm using Sphinx to document parts of my Python code.

I would like to include only a variable from my class in my documentation.

For example, I tried this but without success :

.. literalinclude:: MyClass.py
    :pyobject: MyClass.aVariable
    :language: python

I see only one solution but if my code change in the next release the line numbers of the variable could change :

.. literalinclude:: MyClass.py
    :language: python
    :lines: 2-3

Is there another solution to filter only this variable ?

3
  • 1
    Does it work if you include a method instead of a variable? Commented Jul 22, 2015 at 18:14
  • @mzjn Yes, I tried :pyobject: MyClass.__init__ and it works. Commented Jul 23, 2015 at 7:39
  • 1
    It's the same for me. It might be a bug; I'm not sure. The documentation does not mention variables; it says "you can select a class, function or method to include using the pyobject option" (sphinx-doc.org/markup/…). Commented Jul 23, 2015 at 7:43

1 Answer 1

2

As workaround, I use the start-after and end-before options but I have to add two comments in my code before and after the variables.

For example :

The class :

class MyClass():
    # Start variables
    aVariable = {"a":123 , "b":456}
    # End variables 
        def __init__(self):
            pass

The RST file :

.. literalinclude:: MyClass.py
    :start-after: # Start variables 
    :end-before: # End variables 
    :language: python

It's not perfect, but it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.