Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Jython introspection Service

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample Analysis of Jython introspection Service", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample Analysis of Jython introspection Service".

Introspection Service of Jython

It is often necessary to determine the properties of an object at run time. We call this the object of introspection. The Java platform provides introspection services through java.lang.Class classes and classes in the java.lang.reflect package. These API are powerful, but they are not easy to use. As you guessed it, Jython provides an easier way to introspect.

In Jython, we can use the dir and vars functions to check the binding of any object, such as modules, functions, classes, sequences, maps, and so on. To better understand how it works, consider the following example. The output of each print statement is prefixed with the prefix "..." (and rearranged) to make it easy to read that the dir function returns only the bound name, while the vars function returns the name and value, so when two functions return the same name, we only need to use the vars function, as shown below:

#-empty start-- print "vars:", vars ()... vars: {'_ doc__': None,'_ name__':'_ main__'} x = 1 y = 2 z = 3 l = [x, y, z] d = {x: "xxxx", y: "yyyy", z: "zzzz"} #-local variables-print x, y, z, l, d... 1 2 3 [1, 2 3] {3: 'zzzz', 2:' yyyy', 1: 'xxxx'} #-- Jython introspection: add local variables-- print "vars:", vars ()... vars: {' _ name__':'_ main__','x variables: 1,\. 'dink: {3: 'zzzz', 2:' yyyy', 1: 'xxxx'},' _ _ doc__': None,\. 'name__':: 2,' main__',: [1, 2, 3], 'zonal: 3} import sys #-- Jython introspection: plus import-- print "vars:", vars (). Vars: {' _ _ name__':'_ main__', 'zonal: 3,' lump: [1, 2, 3],\. _ _ doc__': None, 'yearly: 2,' x: 1 'sys': sys module,\... Vars sys: {3: 'zzzz', 2:' yyyy', 1: 'xxxx'}} #-Jython introspection: sys import-- print "vars sys:", vars (sys)... vars sys: {' classLoader':\.

< beanProperty classLoader type: java.lang.ClassLoader at 31845755>

,...... Many values removed...,... 'warnoptions':

< reflected field public static \ ... org.python.core.PyList \ ... org.python.core.PySystemState.warnoptions at 1024901>

} del x, y, z #-- Jython introspection: post delete-- print "vars:", vars ()... vars: {'_ name__':'_ main__', 'lager: [1, 2, 3],' _ doc__': None,\. 'sys': sys module,' y class MyClass: {3: 'zzzz', 2:' yyyy', 1: 'xxxx'}} def func (x, y): return x, y class MyClass (): def _ _ init__ (self, x, y): self.__x = x self.__y = y def method1 (self): return self.x + self.y def method2 (self, x Y): return self.x + self.y + x + y #-Jython introspection: plus function and class-- print "vars:", vars ().... vars: {'func':

< function func at 21569784>

,'_ _ name__':'_ _ main__',\. 'lame: [1, 2, 3],'_ _ doc__': None,\. 'MyClass':

< class __main__.MyClass at 1279942>

,\. 'sys': sys module,' func: {3: 'zzzz', 2:' yyyy', 1: 'xxxx'}} #-- Jython introspection: function-- print "dir:", dir (func) # * dir and vars different here * print "vars:", vars (func)... dir: [_ _ dict__',' _ doc__','_ _ name__', 'func_closure' \... 'func_code',' func_defaults', 'func_doc',' func_globals', 'func_name']. Vars: None #-- Jython introspection: class-- print "vars:", vars (MyClass). Vars: {' _ doc__': None,'_ init__':

< function __init__ at 17404503>

,\. 'method2':

< function method2 at 23511968>

,'_ _ module__':'_ _ main__',\. 'method1':

< function method1 at 28670096>

} myclass = MyClass (1,2) #-Jython introspection: instance-- print "myclass:", myclass print "vars:", vars (myclass)... myclass:

< __main__.MyClass instance at 19014134>

... vars: {'_ MyClass__y': 2,'_ MyClass__x': 1}

Note that dir (x) is generally equated to x.__dict__.keys (), while vars (x) is generally equivalent to x.lifetime equivalent _.

The above is all the contents of the article "sample Analysis of Jython introspection Services". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report