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

Introduction to the special properties of Jython

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the introduction of the special attributes of Jython". In the daily operation, I believe that many people have doubts about the introduction of the special attributes of Jython. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "introduction to the special attributes of Jython"! Next, please follow the editor to study!

List of Jython special properties

The Jython class provides support for several special properties. The most important ones are listed below:

Name function annotation

The writable property of the _ _ dict__ object can be used for the properties of the introspective object

The class of the _ _ class__ object accesses the class of the object (similar to x.getClass () in Java code)

The tuple of the immediate parent class of the _ _ bases__ object can be used for the parent class of the introspective object

Jython special properties-- classes that change existing instances

Unlike most other languages, the class of an existing instance can be changed in Jython. This changes the instance methods that can be used to the methods of the new class, but does not change all pre-existing fields. For example, to change the class of an instance, assign the new class to the special property _ _ class__, as follows:

X = SomeClass () print isinstance (x, SomeClass) # prints: 1 (true) print isinstance (x, SomeOtherClass) # prints: 0 (false): # change the class (that is, the type) of the instance here x.classrooms _ = SomeOtherClass print isinstance (x, SomeClass) # prints: 0 (false) print isinstance (x, SomeOtherClass) # prints: 1 (true) y = SomeOtherClass () print x.

After this change, the x instance will support the SomeOtherClass method instead of the previous SomeClass method. When changing the class of an object, it should be noted that the instance should have the correct properties for the new class.

Special attributes of Jython-other introspection functions

The properties described in the special properties allow for more introspection of the class. In particular, you can use the _ _ dict__ attribute to determine the fields in the methods and instances in the class.

In addition to dir and vars,Jython, there are other functions for introspection classes and instances, as follows:

Function comment

Hasattr (obj, name) tests whether the specified property exists

Getattr (obj, name {, default}) if the attribute exists, get the name of the property, otherwise return the default value (if there is no default, an exception will be thrown)

Setattr (obj, name, value) sets the value of the specified property

Delattr (obj, name) deletes the specified attribute

At this point, the study of "introduction to the special properties of Jython" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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