How to print Docstring of python function from inside the function itself?

def my_func(): “””Docstring goes here.””” print my_func.__doc__ This will work as long as you don’t change the object bound to the name my_func. new_func_name = my_func my_func = None new_func_name() # doesn’t print anything because my_func is None and None has no docstring Situations in which you’d do this are rather rare, but they do …

Read more

How to append whole set of model to formdata and obtain it in MVC

If your view is based on a model and you have generated the controls inside <form> tags, then you can serialize the model to FormData using var formdata = new FormData($(‘form’).get(0)); This will also include any files generated with <input type=”file” name=”myImage” …/> and post it back using $.ajax({ url: ‘@Url.Action(“YourActionName”, “YourControllerName”)’, type: ‘POST’, data: …

Read more

C++ union in C#

You can use explicit field layouts for that: [StructLayout(LayoutKind.Explicit)] public struct SampleUnion { [FieldOffset(0)] public float bar; [FieldOffset(4)] public int killroy; [FieldOffset(4)] public float fubar; } Untested. The idea is that two variables have the same position in your struct. You can of course only use one of them. More informations about unions in struct …

Read more

C# method naming conventions: ToSomething vs. AsSomething

ToDictionary and ToList are prefixed with To because they don’t necessarily preserve the structural identity of the original collection or its properties. Transforming a List<T> into a Dictionary<K, V> creates a collection with a whole new structure. Transforming a HashSet<T> into a List<T> removes the uniqueness property of sets. Methods prefixed with As don’t do …

Read more

C# Generics won’t allow Delegate Type Constraints

A number of classes are unavailable as generic contraints – Enum being another. For delegates, the closest you can get is “: class”, perhaps using reflection to check (for example, in the static constructor) that the T is a delegate: static GenericCollection() { if (!typeof(T).IsSubclassOf(typeof(Delegate))) { throw new InvalidOperationException(typeof(T).Name + ” is not a delegate …

Read more