If a call is made to getattr() for an attribute which is not present and a default value is provided, will the function create the missing attribute and assign it the default value?
Answer
NO, the getattr() function will not create an attribute which is not present. If a default value is provided, it will return that value at the time getattr() is called, otherwise an AttributeError error will be raised if no default value is provided.
#Task: #Inscript.py we have a list of different data types, some strings, some lists, and some dictionaries, all saved in the variable how_many_s .
#For every element in the list, check if the element has the attribute count . If so, count the number of times the string "s" appears in the element. Print this number.
count is a method for the list object type
if you do the following for the different object types, you will see all the methods available for those types:
help([“a”, “b”]) # or can use help(list)
help(18) # or can use help(int)
help(str) # must use this for a string object type
help({}) # or can use help(dict)
if there is an existing object class attribute such as count for the object type, which you can check for using the hasattr() method, then you can call it against that object.