Another neat thing from Python

I really like the Python concept of docstring. Here is an example

class MyClass(object):
   Here I document the class

Which is no different than the xml stuff we have in .net except that in Python, I can now do the following:

print MyClass.__doc__
#which prints "Here I document the class"

Cool, I wish this was that easy in .net. We are already doing some pretty crazy stuff with reflection in .net, add this to the mix and we could really enhance the way we do self documenting code.

Some further reading for those interested:


About Dru Sellers

Sr. Software Engineer at Dovetail Software.
This entry was posted in Small Ideas. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • rourian

    When you first buy a laptop or any electronic device that needs to be charged before you can use it, always charge it to full capacity. , Asus 70-NY81B1000Z
    A laptop will work with only a small bit of power, but if it is not fully charged frequently, it will start to lose its ability to hold a charge. , Bateria Dell Inspiron 2100

  • RhysC

    Hey Dru
    This book I found pretty handy when learning Python. By the end of it you have built a couple of handy libraries and have covered a fair range of the laguage features:

    Cheers and Beers

  • jessica

    the precious ideas you provided do help our team’s investigation for our company, thanks.

  • kevin

    I just wish there was an easy way to access the XML comments via code.

  • Maksym Kozlenko

    Well, C# also has code self-documentation features, which is quite similar to Python
    How about adding the following to your C# method or class:


    /// description goes here

    These comments could also be compiled into CHM or HTML documentation using selection open source (Sandcastle) or commercial tools.

  • Joe

    Here’s an article about how to access XML comments in .NET via code:

    The bottom line is you can use their class and easily fetch the XML comments.

  • drusellers

    @Harry M- Yeah, we can find a way to get close. No doubt. But man, having the ability to bake it into the language with little language noise is pretty cool.

    @Peter: Yeah, Python clearly states they were liberal in their borrowings from other languages. :)

    @DaRage: Yup. One of the bigger (well known) employers where I live is a python shop. – the birth place of django

    @Steve: Yeah, when the source is compiled its not as helpful but since python is a scripted language I personally find it nice. And a possible way to bake some good practices into the code.

    I just wish there was an easy way to access the XML comments via code. @Harry, wanna give that a try. :)

  • Steve Py

    Yeah, if it’s important then attribute it. It’s nothing I’d really want; comments compiled into .Net runtime assemblies. For public API, documentation + usage examples should be provided, anything other than that wouldn’t be any value.

  • http://none Peter

    As often Lisp was there first… 😉

  • DaRage

    I guess you can implement it as an attribute which you can access through reflection.

    Are there any jobs for python?

  • Harry M


    void Main()

    static class DocsExtension{
    public static string Docs(this Type t){
    var docField = t.GetField(“docs”, BindingFlags.NonPublic | BindingFlags.Static);
    if(docField != null){
    return docField.GetValue(null) as string ?? “”;
    return “”;

    class X
    static readonly string docs = “This is a class”;