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() 函数使用方法详解


  • 14
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种支持面向对象编程的编程语言,它提供了丰富的语法和特性来实现面向对象的程序设计。在Python中,可以使用类(class)来定义对象的结构和行为,并通过创建对象(实例化)来使用和操作这些类。 面向对象编程的核心思想是将数据和对数据的操作封装在一个对象中。下面是一些常用的面向对象方法: 1. 类(class):用于定义对象的结构和行为的代码模板。使用class关键字来创建类,并在类中定义属性方法。 2. 对象(object):类的实例化后得到的具体对象。可以通过类名后加括号的方式来创建对象。 3. 属性(attribute):用于描述对象的特征或状态的变量。在类中通过定义变量来表示属性。 4. 方法(method):用于描述对象的行为或操作的函数。在类中通过定义函数来表示方法。 5. 继承(inheritance):子类可以继承父类的属性方法,并可以添加自己的属性方法。可以通过在类定义时添加括号并指定父类来实现继承。 6. 多态(polymorphism):相同的方法可以在不同的对象上产生不同的行为。可以通过重写父类的方法来实现多态。 7. 封装(encapsulation):将数据和对数据的操作进行封装,以实现信息隐藏和安全性。 8. 抽象类和接口(abstract class and interface):抽象类提供了一种不能被实例化的类,只能被用作其他类的基类。接口定义了一组方法,子类必须实现这些方法。 这些方法是面向对象编程中常用的基本概念和技术,它们可以帮助我们更好地组织和管理代码,并实现高效的程序设计。在Python中,面向对象编程是一种强大而灵活的编程范式,可以广泛应用于各种场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值