Python's GetAttr (), SetAttr (), delattr (), hasattr ()

Source: Internet
Author: User

The GetAttr () function is the core function of Python introspection, which is used in the following general context:

Get Object reference GetAttr
GetAttr is used to return an object property, or a method

  1. class A:
  2. def __init__ (self):
  3. self.name = ' zhangjing '
  4. #self. age= ' 24 '
  5. def method (self):
  6. Print"method print"
  7. Instance = A ()
  8. Print getattr (Instance, ' name, ' not find ') #如果Instance object that has the attribute name prints the value of self.name, otherwise printing ' Not find '
  9. Print getattr (Instance, ' age ', ' not find ') #如果Instance object with attribute age prints the value of self.age, otherwise printing ' not ' Find '
  10. Print getattr (A, ' method ', ' default ')
  11. #如果有方法method, otherwise print its address, otherwise print default
  12. Print getattr (A, ' method ', ' Default ') ()
  13. #如果有方法method, run the function and print none otherwise print default


Note: Factory mode can be easily implemented with GetAttr.
Example: A module supports printing in HTML, text, XML and other formats, and calls different functions to implement output in several formats, depending on the formate parameter passed in.

      1. Import Statsout
      2. def output (data, format="text"):
      3. Output_function = GetAttr (statsout, "output_%s"% format)
      4. return output_function (data)

SetAttr ( object, name, value)

This is the counterpart of GetAttr (). The arguments
is an object, a string and an arbitrary value. The string may name an existing
attribute or a new attribute. The function assigns the value to the attribute,
Provided the object allows it. For example, was setattr(x,
‘foobar‘, 123)
equivalent to
x.foobar = 123.

This is the relative getattr (). A parameter is an object, a string, and an arbitrary value. The string may list an existing property or a new property. This function assigns the value to the property. The object allows it to be provided. For example, SetAttr (x, "Foobar", 123) is equivalent to X.foobar = 123.

delattr ( object, name)

This is a relative of setattr (). The arguments is
An object and a string. The string must be the name of the one of the object ' s
Attributes. The function deletes the named attribute, provided the object allows
It. For example, is delattr(x, ‘foobar‘)
Equivalent to del x.foobar .

A set of functions related to SetAttr (). Parameters are made up of an object (remember that everything in Python is an object) and a string. The string argument must be one of the object property names. The function deletes a property specified by string for the obj.delattr(x, ‘foobar‘)=del x.foobar

    • Hasattr is used to determine whether an object has a property.

      Syntax: Hasattr (object, name), BOOL determines whether the object has a Name property and returns a Boolean value.

>>> li=["zhangjing", "Zhangwei"]

>>> GetAttr (li, "pop")
<built-in method Pop of list object at 0x011df6c0>
>>> Li.pop
<built-in method Pop of list object at 0x011df6c0>

>>> Li.pop ()
' Zhangwei '

>>> GetAttr (li, "Pop") ()
' Zhangjing '

>>>getattr (Li, "append") ("Moe")

Python's GetAttr (), SetAttr (), delattr (), hasattr ()

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.