[Technical post] Repeat Annotation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.