Python built-in function (--GETATTR)

Source: Internet
Author: User

English documents:

getattr ( object, name[, default])
Return the value of the named attribute of object. name must is a string. The If the string is the name of the one of the object ' s attributes, and the result is the value of this attribute. For example, was getattr(x, ‘foobar‘) equivalent to x.foobar . If the named attribute does not exist, the default is returned if provided, and otherwise is AttributeError raised.
Description
  
1. The function function is to get the property named name from Object objects, equivalent to calling Object.name.
#Defining class Student>>>classStudent:def __init__(self,name): Self.name=name>>> s = stduent ('Aim')>>> GetAttr (s),'name')#equivalent to calling S.name'Aim'>>>S.name'Aim'

2. Function The third parameter of the default is an optional parameter, if the object in the meaning of the Name property, the value of the Name property is returned, if there is no Name property, the default value is returned, if default does not pass in the value, then an error.

#Defining class Student>>>classStudent:def __init__(self,name): Self.name=name>>> GetAttr (s),'name')#existence attribute name'Aim'>>> GetAttr (s),' Age', 6)#No attribute age exists, but default value is provided, return default value6>>> GetAttr (s),' Age')#attribute age not present, default value not provided, error calledTraceback (most recent): File"<pyshell#17>", Line 1,inch<module>GetAttr (s),' Age') Attributeerror:'stduent'object has no attribute' Age'

Python built-in function (--GETATTR)

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.