python 获得对象的所有属性和方法

dir(Object)

例如:XBLOCK对象有下例属性和方法,可以通过dir(self) 获得

dir[self]=['__class__', '__delattr__', '__dict__', '__doc__', '__fo
rmat__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclassh
ook__', '__weakref__', '_add_field', '_asides', '_child_cache', '_class_tags', '_clear_dirty_fields', '_combined_services', '_create_metadata_editor_info', '_deprecated_per_instance_field_da
ta', '_dirty_fields', '_edit_info', '_field_data', '_field_data_cache', '_get_fields_to_save', '_get_user_partition', '_load_class_entry_point', '_parent_block', '_parent_block_id', '_reset_
dirty_field', '_runtime', '_services_requested', '_set_field_if_present', '_unwrapped_field_data', 'add_aside', 'add_children_to_node', 'add_xml_to_node', 'always_recalculate_grades', 'annot
ation_storage_url', 'annotation_token_secret', 'bind_for_student', 'category', 'chrome', 'clear_child_cache', 'count', 'course_edit_method', 'course_id', 'course_version', 'days_early_for_be
ta', 'default_reset_button', 'default_tab', 'display_name', 'display_name_with_default', 'display_name_with_default_escaped', 'displayable_items', 'due', 'editable_metadata_fields', 'edited_
by', 'edited_on', 'edxnotes', 'edxnotes_visibility', 'entry_point', 'extra_entry_points', 'fields', 'force_save_fields', 'format', 'get_asides', 'get_child', 'get_child_by', 'get_children',
'get_content_titles', 'get_display_items', 'get_explicitly_set_fields_by_scope', 'get_icon_class', 'get_parent', 'get_progress', 'get_public_dir', 'get_required_module_descriptors', 'get_res
ources_dir', 'get_score', 'giturl', 'graceperiod', 'graded', 'group_access', 'handle', 'handler', 'has_cached_parent', 'has_children', 'has_children_at_depth', 'has_dynamic_children', 'has_s
core', 'has_support', 'hide_from_toc', 'icon_class', 'in_entrance_exam', 'increment_count', 'index_dictionary', 'is_draft', 'json_handler', 'load_class', 'load_classes', 'load_tagged_classes
', 'location', 'logger', 'matlab_api_key', 'max_attempts', 'max_score', 'merged_group_access', 'name', 'needs', 'non_editable_metadata_fields', 'open_local_resource', 'parent', 'parse_xml',
'plugin_name', 'pre_or_next', 'public_dir', 'published_by', 'published_on', 'readFile', 'register_temp_plugin', 'render', 'rerandomize', 'reset_key', 'resource_string', 'resources_dir', 'run
time', 'save', 'scope_ids', 'self_paced', 'service_declaration', 'show_in_read_only_mode', 'show_reset_button', 'showanswer', 'source_file', 'start', 'static_asset_path', 'student_view', 'su
bmit', 'subtree_edited_by', 'subtree_edited_on', 'supports', 'system', 'tag', 'tags', 'tooltip_title', 'ugettext', 'unmixed_class', 'url_name', 'use_latex_compiler', 'user_partitions', 'vali
date', 'video_bumper', 'video_speed_optimizations', 'visible_to_staff_only', 'wants', 'workbench_scenarios', 'xblock_kvs', 'xml_element_name', 'xml_text_content', 'xmodule_runtime', 'xqa_key
']

要判断对象是否有某个属性,可以用:

hasattr(Object, "name")

如:

hasattr(self,”diaplay_name”)

会返回True

 

要获得某个属性的值:self.display_name

参考:

http://www.cnblogs.com/cenyu/p/5713686.html

hasattr() getattr() setattr() 函数使用方法详解

    原文作者:YanniZhang的博客
    原文地址: https://blog.csdn.net/jenyzhang/article/details/73739824
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞