4,853 questions
526
votes
21
answers
845k
views
Python dictionary from an object's fields
Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this:
>>> class Foo:
... bar = 'hello'
... baz = 'world'
...
...
230
votes
5
answers
86k
views
Get the name of the currently executing method
$0 is the variable for the top level Ruby program, but is there one for the current method?
217
votes
8
answers
126k
views
What exactly is metaprogramming?
I was reading an article on TheServerSide on ployglot programming on the Java platform. Some comments in the article refer to metaprogramming as the ability to generate code (perhaps on the fly).
Is ...
183
votes
4
answers
119k
views
Calling a Method From a String With the Method's Name in Ruby
How can I do what they are talking about here, but in Ruby?
How would you do the function on an object? and how would you do a global function (see jetxee's answer on the post mentioned)?
EXAMPLE ...
181
votes
10
answers
84k
views
What are good uses of SFINAE?
I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE?
179
votes
5
answers
83k
views
Is it possible to implement dynamic getters/setters in JavaScript?
I am aware of how to create getters and setters for properties whose names one already knows, by doing something like this:
// A trivial example:
function MyObject(val){
this.count = 0;
this....
166
votes
4
answers
67k
views
How do you pass arguments to define_method?
I would like to pass an argument(s) to a method being defined using define_method, how would I do that?
166
votes
21
answers
113k
views
Conveniently Declaring Compile-Time Strings in C++
Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, ...
153
votes
9
answers
159k
views
What does send() do in Ruby?
Can someone please tell me what the following snippet
obj.send("#{method_name}")
is and does?
153
votes
5
answers
11k
views
Can a line of Python code know its indentation nesting level?
From something like this:
print(get_indentation_level())
print(get_indentation_level())
print(get_indentation_level())
I would like to get something like this:
1
2
3
Can the code read ...
148
votes
6
answers
38k
views
Is it possible to figure out the parameter type and return type of a lambda?
Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how?
Basically, I want lambda_traits which can be used in following ways:
auto lambda = [](int i) { return ...
132
votes
1
answer
18k
views
How Pony (ORM) does its tricks?
Pony ORM does the nice trick of converting a generator expression into SQL. Example:
>>> select(p for p in Person if p.name.startswith('Paul'))
.order_by(Person.name)[:2]
SELECT &...
131
votes
3
answers
61k
views
Objective-C class -> string like: [NSArray className] -> @"NSArray"
I am trying to get a string name of a class from the class object itself.
// For instance
[NSArray className]; // @"NSArray"
I have found object_getClassName(id obj) but that requires an instance be ...
126
votes
8
answers
78k
views
Best introduction to C++ template metaprogramming? [closed]
Static metaprogramming (aka "template metaprogramming") is a great C++ technique that allows the execution of programs at compile-time. A light bulb went off in my head as soon as I read this ...
124
votes
13
answers
9k
views
How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?
In a recent interview, I was asked a really strange question. The interviewer asked me how can I compute 1+2+3+...+1000 just using compiler features. This means that I am not allowed to write a ...