Python3 hasattr (), GetAttr (), SetAttr (), delattr () functions, and sample code

Source: Internet
Author: User
This article through the sample code to give you a detailed introduction of Python3 in Hasattr (), GetAttr (), SetAttr (), delattr () function, very good, with reference value, need to refer to a friend

Hasattr () function

The Hasattr () function is used to determine if a corresponding property is included

Grammar:

hasattr(object,name)

Parameters:

object--Object

name--String, property name

return value:

Returns True if the object has this property, otherwise false

Example:


class people:  country= ' China '  def __init__ (self,name):    self.name=name  def people_info (self):    Print ('%s is xxx '% (self.name)) obj=people (' AAA ') print (hasattr (people, ' country ') ") #返回值: Trueprint (' Country ' in people. __dict__) #返回值: Trueprint (hasattr (obj, ' people_info ')) #返回值: Trueprint (people.__dict__) ##{' __module__ ': ' __main__ ', ' Country ': ' China ', ' __init__ ': <function people.__init__ at 0x1006d5620>, ' People_info ': <function People.people_info at 0x10205d1e0>, ' __dict__ ': <attribute ' __dict__ ' of ' people ' objects>, ' __weakref__ ': < Attribute ' __weakref__ ' of ' people ' objects>, ' __doc__ ': None}


GetAttr () function

Describe:

The GetAttr () function is used to return an object property value

Grammar:

getattr(object,name,default)

Parameters:

object--Object

name--strings, Object properties

default--The default return value, if not supplied, Attributeerror is triggered when no property is available.

return value:

Return object property values


class people:  country= ' China '  def __init__ (self,name):    self.name=name  def people_info (self):    Print ('%s is xxx '% (self.name)) obj=getattr (People, ' country ') print (obj) #返回值China #obj=getattr (People, ' Countryaaaaaa ') #print (obj) #报错 # File "/getattr () function. py", line +, in <module>#   obj=getattr (People, ' Countryaaaaaa ') # Attributeerror:type object ' People ' has no attribute ' countryaaaaaa ' obj=getattr (People, ' Countryaaaaaa ', None) print (obj) #返回值None


SetAttr () function

Describe:

The SetAttr function, which is used to set the property value, must exist

Grammar:

setattr(object,name,value)

Parameters:

object--Object

name--strings, Object properties

value--Property value

return value:

No


class people:  country= ' China '  def __init__ (self,name):    self.name=name  def people_info (self):    Print ('%s is xxx '% (self.name)) obj=people (' AAA ') setattr (People, ' X ', 111) #等同于People. X=111print (people.x) #obj. age= 18setattr (obj, ' age ', +) print (obj.__dict__) #{' name ': ' AAA ', ' Age ': 18}print (people.__dict__) #{' __module__ ': ' __main __ ', ' Country ': ' China ', ' __init__ ': <function people.__init__ at 0x1007d5620>, ' people_info ': <function People . People_info at 0x10215d1e0>, ' __dict__ ': <attribute ' __dict__ ' of ' people ' objects>, ' __weakref__ ': < Attribute ' __weakref__ ' of ' people ' objects>, ' __doc__ ': None, ' x ': 111}


Delattr () function

Describe:

The DELATTR function is used to delete properties

Delattr (x, ' Foobar) equals del X.foobar

Grammar:

setattr(object,name)

Parameters:

object--Object

name--must be an object's property

return value:

No

Example:


class people:  country= ' China '  def __init__ (self,name):    self.name=name  def people_info (self):    Print ('%s is xxx '% (self.name)) delattr (People, ' country ') #等同于del People.countryprint (people.__dict__) {' __module__ ': ' __main__ ', ' __init__ ': <function people.__init__ at 0x1006d5620>, ' people_info ': <function People.people_ info at 0x10073d1e0>, ' __dict__ ': <attribute ' __dict__ ' of ' people ' objects>, ' __weakref__ ': <attribute ' __we akref__ ' of ' people ' objects>, ' __doc__ ': None}


Additional examples:


Class Foo:  def run (self):    while True:      cmd=input (' cmd>>: '). Strip ()      if Hasattr (self,cmd):        Func=getattr (Self,cmd)        func ()  def download (self):    print (' Download ... ')  def upload (self):    Print (' Upload ... ') # Obj=foo () # Obj.run ()

Related Article

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.