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?
A Python script can find out about the type, class, attributes and methods of an object. This is referred to as reflection or introspection.
You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.
It's very bad because it ties your UI to your method names, which should be completely unrelated. Making an seemingly innocent change later on can have unexpected disastrous consequences. Using reflection is not a bad practice. Doing this sort of thing is a bad practice.
Reflection is a process of exploring and examining ourselves, our perspectives, attributes, experiences and actions / interactions. It helps us gain insight and see how to move forward. Reflection is often done as writing, possibly because this allows us to probe our reflections and develop them more thoughtfully.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With