Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

Can someone please explain the syntax of the getatter docstring in python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The getattr() function returns the value of the specified attribute from the specified object.

Lets create a class Person first which have attributes name, age and country.

When we call getattr() it will take 3 arguments as (object, attribute, default). Here we gave the arguments as Object=Person, attribute='age'. Default is used to print any statement when the attribute is not found.

class Person:
  name = "John"
  age = 36
  country = "Norway"

x = getattr(Person, 'age')

Here the answer given by this is 36.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...