I have already written this article, but I can't find it all by mistake. I had to rewrite it. :'(
The last time I pulled an annotation. Later, gigix added an article annotation vs. configuration. I downloaded the speech recording and lecture of Xu X recommended by him (bjug. He's right,Annotation is a custom metadata, which is not the same as configuration..
A few days ago, gigix made another article about using Ruby to implement the custom metadata function. He commented:"Compared with the attribute/annotation of C # and Java, Ruby is quite elegant.".
I didn't quite agree at first, and I had some misunderstandings about this (because the original text is lost, I cannot remember it now ). Later, I discussed with Ling Hu. Ling Hu also implemented the same function using python.Program-- Because Google Talk only saves recent chat records, I did not save this program. Later I wrote one myself, and the method may be slightly different.
Ling Hu later pointed out:Dynamic language functions are very powerful. All you need is an idea. If you have an idea, you can achieve it in a beautiful way.. (Lost in general)
Later, I realized that attribute/annotation must be introduced in C # and Java. As I encountered when using Delphi
You need to maintain some custom metadata-of course, the result without attribute/annotation is even more ugly.
At first, I only saw the convenience of attribute/Annotation on the surface, and did not see the greater superiority of dynamic languages in the aspect of meta-programming (I have read an introduction
Python metaprogrammingArticle). Linghu went back and studied how to use attribute to implement the custom metadata function under. net. He
Think:This implementation separates metadata from class, which is not elegant.. (Lost in general)
Appendix: The program implemented in Python:
class user:
def _ init _ (self, name, age):
try:
invalid_names = self. _ class __. invalid_names
T attributeerror:
invalid_names = []
If (name in invalid_names):
raise exception ("cannot use this name:" + name)
self. name = Name
self. age = age
class userimpl (User):
invalid_names = ["fuck ", "damn"] # meta-data passed in class attribute
If _ name _ = "_ main __":
U1 = userimpl ("gigix", 24) # Will success
try:
u2 = userimpl ("fuck", 24) # will raise a runtimeerror
Except t exception:
pass
U3 = user ("fuck", 24) # Will success, user class not forbid any name