10

I've written C# and the mantra coming from on high seems to be "never use reflection in production code". I have used it for test code, but never anything that runs in the wild. All the arguments seem reasonable, and there's always a way to do it by adding another layer of abstraction or design pattern or whatever.

Now I'm starting to write some serious Python code, I wonder if the same principle applies. It seems that python is designed with reflection in mind. Modules and classes store members in an easily accessible dictionary. Django's models' Meta classes, for example take strings to reference members.

I could write C#/Java in Python but I really don't want to. I still firmly believe in 'no reflection' for said languages. Is the Python way just fundamentally different?

Joe
  • 46,419
  • 33
  • 155
  • 245
  • 1
    This is funny, because Reflection is so heavily used in both .NET and 3rd party libraries, that "not using reflection" would mean "not using .NET itself". – Euphoric Jan 20 '11 at 10:55
  • Yes, .NET uses reflection to do all kinds of things. But another of the principles was 'trust .NET and other libraries to do what they promise'. Which is fine if you stick to well-tested libraries. We should be concerned primarily about bugs in our own code. – Joe Jan 20 '11 at 11:00
  • I think this can be said about any piece of technology, not only Reflection. As long as you know what are you doing, and know its ups and downs I dont find a reason not to use it. – Euphoric Jan 20 '11 at 11:12
  • Where did you hear not to use reflection? The one argument I read in the answers was performance, though given used where appropriate that should not make any measurable difference in runtime. The only other thing I can imagine is obfuscation and copy protection, where everything is suddenly not as expected, for example entities having completely different names. – Andreas Reiff Nov 06 '15 at 13:25
  • This is going back a few years now! I'm guessing it would have been in the Microsoft coding style / static analysis guidelines. – Joe Nov 06 '15 at 14:12

5 Answers5

18

As a dynamic language Python is fundamentally different than statically typed languages, so everything is reflection in it :-) Also never use reflection in production code (for static languages) seems a bit extreme to me.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 7
    Agreed. Saying _never_ to use something and instead wasting time lots debugging/re-structuring code instead of using built in-functionality seems a bit silly to me. Of course if possible it's best to avoid reflection, but not always. – Rob Jan 20 '11 at 09:07
  • I know this is an old comment but I agree. Also never use reflection in time sensitive production code is our mantra. Reflection in .net is very slow. – Justin Aug 02 '11 at 20:37
6

I think that the "no reflection in production code" is not correct in c#.
Reflection often allows the programmer to do things otherwise impossible.
I would say "no reflection for non-public members in production code" and "use reflection with care!" if not used correctly, you could lose performance. Used correctly could make you gain performance (just think about static reflection) Don't use reflection for massivly called code.
Python instead is a dynamic language. All concepts are different. The normality (and the correct way to do it) is using the techinques you are talking about.

nemenos
  • 877
  • 1
  • 10
  • 23
  • What do you mean by "static reflection", nemenos? – Nickolodeon Jan 20 '11 at 09:16
  • In a very fast way to explain it, it's a way to compile information gathered by reflection. This way you pay the cost of reflection only once, and the compiled result is almost as fast as purely compiled code. – nemenos Jan 20 '11 at 09:31
3

Yes, in that aspect, Python developement is fundametally different.

Reflection in C#/Java refers to the ability of the runtime to know things about the code it's running, and to take decisions based on that information.

Since Python uses dynamic typing, any type discovery is delegated to the runtime and not the compile time, so basically that means, that any Python program must use reflection in order to work, only it's not called reflecting, it's called running the program :)
In addition, the Python philosophy embraces the dynamic nature of execution, so you should not hesitate to use it to your advantage.

P.S. While one should avoid using reflection in tight loops, and one should be aware that reflection is one or two orders of magnitude slower, one should not be afraid to use it when it's the right tool for the job.

SWeko
  • 30,434
  • 10
  • 71
  • 106
  • Nice answer. But there is a difference between saying `myobject.foo()` and `x = getattr(myobject, "foo"); x();`. Even if it is only cosmetic. In the first the `foo()` is compiled in statically. In the second, the string could be produced any number of ways. – Joe Jan 20 '11 at 10:39
  • 1
    Eh, potato / solanum tuberosum... in python, niether is statically compiled, so they are more or less equivalent. – SWeko Jan 20 '11 at 10:41
0

IMO the reason for me about avoiding using reflection in production code is the fact than reflection could make code really harder to maintain and debug.

ykatchou
  • 3,667
  • 1
  • 22
  • 27
  • If it's littered with reflection, sure. For something like an object factory or a structure map, I don't quite agree. – Rob Jan 20 '11 at 09:09
0

Reflection is very advanced and powerfull technology. But it's slow. You can use reflection, but not to often. So CLR developers added one more cool type to .NET 4.0 - dynamic type. I strongly recommend you to refer to some documents or books for more info. F.e. CLR via C#.

dynamic a = SomeFooInterface.GetsomeObjectWithUnknownInterface(); 
// returns object, for example, from another assembly. or some unknown instance.
// But you know that there is method int GetValue() inside. 
// So you can call this method without any casts!
int myValue = a.GetValue();
// This method is much faster then reflection!

CLR via C#:

There are also many occasions when a program has to act on information that it doesn’t know about until it is running. While you can use type-safe programming languages (like C#) to interact with this information, the syntax tends to be clumsy, especially since you tend to work a lot with strings, and performance is hampered as well. If you are writing a pure C# application, then the only occasion you have for working with runtime-determined information is when you are using reflection (discussed in Chapter 23). However, many developers also use C# to communicate with components that are not implemented in C#. Some of these components could be .NET-dynamic languages such as Python or Ruby, or COM objects that support the IDispatch interface (possibly implemented in native C or C++), or HTML Document Object Model (DOM) objects (implemented using various languages and technologies). Communicating with HTML DOM objects is particularly useful when building a Microsoft Silverlight application. To make it easier for developers using reflection or communicating with other components, the C# compiler offers you a way to mark an expression’s type as dynamic. You can also put the result of an expression into a variable and you can mark a variable’s type as dynamic. This dynamic expression/variable can then be used to invoke a member such as a field, a property/indexer, a method, delegate, and unary/binary/conversion operators. When your code invokes a member using a dynamic expression/variable, the compiler generates special IL code that describes the desired operation. This special code is referred to as the payload. At runtime, the payload code determines the exact operation to execute based on the actual type of the object now referenced by the dynamic expression/variable.

Sasha Reminnyi
  • 3,442
  • 2
  • 23
  • 27
  • Thanks for that. You could have just said "CLR via C# page X", I have a copy here! This question, however, is about Python not C#. – Joe Jan 20 '11 at 10:07
  • The quote is for someone that could not have the copy. And you wrote you can use other language, so i decided to advice you C# back :). – Sasha Reminnyi Jan 20 '11 at 10:23
  • >// This method is much faster then reflection! Funny, because only way to execute code like this on static type is to use reflection. – Euphoric Jan 20 '11 at 10:52
  • @LexRama - sorry I might not have made myself clear. When I said "I could write C#/Java in Python" I was quoting the famous joke "you can write Fortran in any language" - http://www.pbm.com/~lindahl/real.programmers.html , http://queue.acm.org/detail.cfm?id=1039535 – Joe Jan 20 '11 at 16:07