Never use reflection in production code! What abou

2019-04-24 08:17发布

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?

5条回答
祖国的老花朵
2楼-- · 2019-04-24 08:57

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.

查看更多
We Are One
3楼-- · 2019-04-24 08:58

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.

查看更多
狗以群分
4楼-- · 2019-04-24 09:07

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.

查看更多
叛逆
5楼-- · 2019-04-24 09:08

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.

查看更多
迷人小祖宗
6楼-- · 2019-04-24 09:12

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.

查看更多
登录 后发表回答