Tag Archives: XXX object has no attribute XXX

How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’

AttributeError: ' dict ' object has no attribute ' item '

This error means that python cannot find the attributes of the corresponding object, and the beginners don’t know enough about the function object, which leads to errors

Original code:

favorite_languages ​​= {
     ' jen ' : ' python ' ,
     ' sarah ' : ' c ' ,
     ' edward ' : ' ruby ' ,
     ' phil ' : ' python ' ,
    }
for name,language in favorite_languages.item():
     print (name.title()+ " 's favorite language is " +language.title()+ " . " )

The text editors we use generally use obvious colors as hints:

 

 Modified code:

favorite_languages ​​= {
     ' jen ' : ' python ' ,
     ' sarah ' : ' c ' ,
     ' edward ' : ' ruby ' ,
     ' phil ' : ' python ' ,
    }
for name,language in favorite_languages.items():
     print (name.title()+ " 's favorite language is " +language.title()+ " . " )

operation result:

1 Jen ' s favorite language is Python. 
2 Sarah ' s favorite language is C. 
3 Edward ' s favorite language is Ruby. 
4 Phil ' s favorite language is Python. 
5 Sarah ' s favorite language is C.

 Summarize the commonly used Python error types as follows:

  • ZeroDivisionError-divide (or modulo) zero (all data types) 
  • ValueError-Invalid parameter passed in 
  • AssertionError-assertion statement failed 
  • StopIteration-the iterator has no more values 
  • IndexError-there is no such index in the sequence (index) 
  • IndentationError-indentation error 
  • OSError-input/output operation failed 
  • ImportError——Failed to import module/object 
  • NameError-Object not declared/initialized (no attributes) 
  • AttributeError- indicating that the object does not have this attribute
  • GeneratorExit-an exception occurs in the generator to notify the exit 
  • TypeError-invalid operation on the type 
  • KeyboardInterrupt-user interrupt execution (usually input ^C) 
  • OverflowError-Numerical operation exceeds the maximum limit 
  • FloatingPointError-floating point calculation error 
  • BaseException-the base class of all exceptions 
  • SystemExit-interpreter requests to exit 
  • Exception-the base class for general errors 
  • StandardError-the base class for all built-in standard exceptions 
  • ArithmeticError-the base class for all numerical calculation errors 
  • EOFError-there is no built-in input, reaching the EOF mark 
  • EnvironmentError-the base class for operating system errors 
  • WindowsError-system call failed 
  • LookupError-the base class for invalid data query 
  • KeyError-there is no such key in the map 
  • MemoryError-memory overflow error (not fatal to the Python interpreter) 
  • UnboundLocalError-access to uninitialized local variables 
  • ReferenceError-Weak reference attempts to access objects that have been garbage collected 
  • RuntimeError-general runtime error 
  • NotImplementedError-method not yet implemented 
  • SyntaxError Python-syntax error 
  • TabError-Tab and space are mixed 
  • SystemError-general interpreter system error 
  • UnicodeError-Unicode related errors 
  • UnicodeDecodeError-Unicode decoding error 
  • UnicodeEncodeError-Unicode encoding error 
  • UnicodeTranslateError-Unicode conversion error

The following are warning types 

  • Warning-the base class of warnings 
  • DeprecationWarning-warning about deprecated features 
  • FutureWarning-a warning that the semantics of the structure will change in the future 
  • OverflowWarning-old warning about automatic promotion to long integer (long) 
  • PendingDeprecationWarning-warning about the feature will be deprecated 
  • RuntimeWarning-warning of suspicious runtime behavior 
  • SyntaxWarning-warning of suspicious syntax 
  • UserWarning-warning generated by user code

Numpy.exp Function Error ‘Float’ object has no attribute ‘exp’

Problem Description:

When python is using a custom sigmoid function, input X as a matrix, there will be a situation where’Float’ object has no attribute’exp’.

def sigmoid(inp):
return 1.0/(1 + np.exp(-inp))

It is found that it is no problem to manually generate the matrix data into this function, and then find it by looking up the numpy.mat function

numpy.mat(data, dtype=None)[source]

Interpret the input as a matrix.

Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).

Parameters:
data: array_like

Input data.

dtype: data-type

Data-type of the output matrix.

Returns:
mat: matrix

data interpreted as a matrix.

The default dtype is None, so when the matrix is ​​generated, the dtype is added to the type to solve the problem. For example, Xmat = numpy.mat(_x, dtype=float), and then Xmat was brought into the sigmoid function, no problem was found.


AttributeError: 'Float' object has no attribute 'exp'