Code that sets the default value in case of AttributeError

Be aware that you will get an AttributeError when writing code in Python

In the code below, AttributeError occurs because the name attribute is not set in the Test class.

test_code.py


class Test:
     def __init__(self, cd):
            self.cd = cd


test = Test("c001")
print(test.cd) #c001
print(test.name) # AttributeError

When writing code that anticipates that the attribute does not exist, it is good to use getattr as follows.

https://docs.python.org/3.5/library/functions.html#getattr

test_code2.py


class Test:
     def __init__(self, cd):
            self.cd = cd


test = Test("c001")
print(test.cd) #c001
print(getattr(test, "name", "default value")) #Since there is no attribute name, the default value is displayed

Recommended Posts

Code that sets the default value in case of AttributeError
Make the default value of the argument immutable
Find the divisor of the value entered in python
Search by the value of the instance in the list
Isn't there a default value in the dictionary?
[Python] Summary of functions that return the index that takes the closest value in the array
# Function that returns the character code of a string
Make the default value of the argument immutable (article explanation)
[Golang] Specify an array in the value of map
The story that the return value of tape.gradient () was None
A note on the default behavior of collate_fn in PyTorch
Partial in case of trouble
Explain the code of Tensorflow_in_ROS
Get the value while specifying the default value from dict in Python
It is said that Fabric cannot get the status code in the middle of the pipe in the shell script.
When incrementing the value of a key that does not exist
Be careful when specifying the default argument value in Python3 series
How to implement Java code in the background of RedHat (LinuxONE)
I wrote the code to write the code of Brainf * ck in python
A function that measures the processing time of a method in python
The story of participating in AtCoder
About the return value of pthread_mutex_init ()
About the return value of the histogram.
The story of the "hole" in the file
The meaning of ".object" in Django
About the case that the speaker did not make sound in Ubuntu LTS 20.04
The case that the installation of pip became easier before I knew it
Code reading of faker, a library that generates test data in Python
I tried to display the altitude value of DTM in a graph
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
This and that of the inclusion notation.
Handling of character code of file in IronPython
The story of an error in PyOCR
Implement part of the process in C ++
[Bash / linux] Notes in case of trouble
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
The basics of running NoxPlayer in Python
Get the value of the middle layer of NN
In search of the fastest FizzBuzz in Python
[Python] Get the character code of the file
Get the EDINET code list in Python
A code that corrects the yoon / sokuon (sokuon)
A collection of Numpy, Pandas Tips that are often used in the field
Talking about the features that pandas and I were in charge of in the project
To output a value even in the middle of a cell with Jupyter Notebook
Find the index of items that match the conditions in the pandas data frame / series
Note that the Logistic Regression solver has changed its default value to lbfgs.
Create a BOT that displays the number of infected people in the new corona
[Django] Create a form that automatically fills in the address from the zip code
Compare the sum of each element in two lists with the specified value in Python
How to find the coefficient of the trendline that passes through the vertices in Python
[Discode Bot] I created a bot that tells me the race value of Pokemon
Correspondence of the event that the result of form.is_valid () is always False in Django2 system