【问题标题】:Find Python Class Attributes In Shell在 Shell 中查找 Python 类属性
【发布时间】:2015-04-21 20:37:24
【问题描述】:

我开始从 Ruby on Rails 学习 Python。

我想知道是否有办法通过 Python Shell 查看属于某个类的属性?

在 Ruby 中,我们只需进入控制台并可以键入类的名称并显示其属性。有没有办法在 Python 中做到这一点?

Ruby Shell

2.2.0 :001 > User
class User < ActiveRecord::Base {
                        :id => :integer,
                      :name => :string,
                     :email => :string,
        :encrypted_password => :string,
      :reset_password_token => :string,
    :reset_password_sent_at => :datetime,
       :remember_created_at => :datetime,
             :sign_in_count => :integer,
        :current_sign_in_at => :datetime,
           :last_sign_in_at => :datetime,
        :current_sign_in_ip => :string,
           :last_sign_in_ip => :string,
        :confirmation_token => :string,
              :confirmed_at => :datetime,
      :confirmation_sent_at => :datetime,
         :unconfirmed_email => :string,
                :created_at => :datetime,
                :updated_at => :datetime,
                      :role => :string,
                       :bio => :text,
                       :sex => :string,
                     :title => :string,
                :department => :string,
                  :keywords => :text,
                 :education => :text,
                    :skills => :text,
          :invitation_token => :string,
     :invitation_created_at => :datetime,
        :invitation_sent_at => :datetime,
    :invitation_accepted_at => :datetime,
          :invitation_limit => :integer,
             :invited_by_id => :integer,
           :invited_by_type => :string,
         :invitations_count => :integer
}
2.2.0 :002 > 

【问题讨论】:

    标签: python ruby read-eval-print-loop introspection


    【解决方案1】:

    使用dir()函数:

    >>> print dir(str)
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    

    它适用于任何对象,包括类、内置函数和函数。

    如果您希望输出格式更像您的 ruby​​ 示例:

    In [169]: for attrib in dir(str):
        print "{0:18} =>  {1}".format(attrib, type(getattr(str, attrib)))
    

    输出:

    __add__            =>  <type 'wrapper_descriptor'>
    __class__          =>  <type 'type'>
    __contains__       =>  <type 'wrapper_descriptor'>
    __delattr__        =>  <type 'wrapper_descriptor'>
    __doc__            =>  <type 'str'>
    __eq__             =>  <type 'wrapper_descriptor'>
    __format__         =>  <type 'method_descriptor'>
    __ge__             =>  <type 'wrapper_descriptor'>
    __getattribute__   =>  <type 'wrapper_descriptor'>
    ...
    

    【讨论】:

    • 有没有办法只获取我创建的属性?就像如果用户只由电子邮件组成,它只会返回电子邮件?就像我上面的例子一样,只返回我创建的属性
    • 可以过滤掉以“__”开头的属性,否则dir()不区分继承的属性和用户定义的属性。
    【解决方案2】:

    您实际上不需要打印。你可以简单地使用 dir()。

    【讨论】:

    • 我使用 print 是因为我希望输出全部在一行上 -- 我不想占用页面上的 71 行。
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多