AttributeError, why? - python

I'm writing a program that solves a tetravex and I encountered this error:
AttributeError: 'list' object has no attribute 'PlacementList'.
I tried everything I know but don't know why I'm getting that error. Could you please tell me what I did wrong?
This is a pastebin link to my code: http://pastebin.com/d1WdbCUu

It happens when you are trying to get PlacementList attribute of a list, obviously.
Here is the example:
>>> a = []
>>> a.PlacementList
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
a.PlacementList
AttributeError: 'list' object has no attribute 'PlacementList'
Just find the code where something similar happens - you are trying to get PlacementList attribute of the object that can be of type list.

Related

Is it possible to get an error when accidentally assigned wrong type in python?

For example I have next code:
name = str('John Doe')
Let's imagine after I assign to name = 1, but code still valid.
Is it possible to get an error in this case in Python or within some special tool?
Python is dynamically typed, so you can assign pretty much anything to anything. This would work:
name = 1
name = "Bill"
You can add in type checking with mypy
I'm not sure if you are trying to raise an error, or just wondering if an error might arise if you change the value.
Raise an error
you could check the type using isinstance(obj, class) and raise TypeError(msg).
Is it possible to get an error?
Not directly, but you could as a consequence get an error if you use string operations/methods on an integer.
Example:
>>> name = "john doe"
>>> name = 1
>>> name.split(" ")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'split'

AttributeError: 'DataFrame' object has no attribute 'items' (pandas DataFrame error)

I've created a pandas DataFrame with data from loops (data saved here). Now, I want to iterate over this DataFrame. However, when I try to access the items() function like this:
frame = pandas.read_csv(data_path + '/file1.csv')
frame.items()
I get this error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\generic.py", line 2668, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'items'
This is especially weird since PyCharm offers me the option to fill in items if I just type frame.it
What does this mean?
Check your pycharm interpreter. Normally the attribute 'items' works well in Python 3.x but gives the error "AttributeError: 'DataFrame' object has no attribute 'items'" in Python 2.7.
I also faced the same error until I realized that I was using Python 2.7 instead of Python 3.9 that I intended to use.

Why does the following attribute error occur?

I get the following error when trying to execute the following in Python with the PuLP library
for i in range(0, items):
print('x{0} = {1}'.format(i+1, value('x{0}'.format(i+1))))
That is my code which throws the following error
Traceback (most recent call last):
File "C:/Python34/knapsack.py", line 81, in <module>
print('x{0} = {1}'.format(i+1, value('x{0}'.format(i+1))))
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1990, in value
else: return x.value()
AttributeError: 'str' object has no attribute 'value'
My question is why does this not work. Splitting the print statement tends to work correctly.
The value method expects a PuLP variable object, not a string with the same name as one.
In order to evaluate your string back to a python variable, you need to eval it, so that would look like.
value(eval('x{0}'.format(i+1)))

List comprehension with single object in python

I have a list like so,
<Node: uuid=c1f462dde9ff6eb80b4197db5972c4feaf29e4e7, name=new-node, state=PENDING, public_ips=[], private_ips=['10.0.2.217'], provider=Amazon EC2 (ap-northeast-1) ...>
Node is a single object in the list, and I want to access only the name attribute from the object. I tried this,
[i.name for i in my_list]
It is showing error as:
Traceback (most recent call last):
File "create.py", line 50, in <module>
name = [i.name for i in sample]
TypeError: 'Node' object is not iterable
How to fetch only name attribute from this object?
The error is pretty self-explanatory, you are trying to iterate over something that is not iterable. i.e. you cannot loop over it.
You simply need to access it as sample.name

python "'NoneType' object has no attribute 'encode'"

I wrote this tiny Python snippet that scrapes a feed and prints it out. When I run the code, something in the feed triggers the error message you see here as my question. Here's the complete console output on error:
> Traceback (most recent call last):
> File "/home/vijay/ffour/ffour5.py",
> line 20, in <module>
> myfeed() File "/home/vijay/ffour/ffour5.py", line
> 15, in myfeed
> sys.stdout.write(entry["title"]).encode('utf-8')
> AttributeError: 'NoneType' object has
> no attribute 'encode'
> sys.stdout.write(entry["title"]).encode('utf-8')
This is the culprit. You probably mean:
sys.stdout.write(entry["title"].encode('utf-8'))
(Notice the position of the last closing bracket.)
Lets try to clear up some of the confusion in the exception message.
The function call
sys.stdout.write(entry["title"])
Returns None. The ".encode('utf-8')" is a call to the encode function on what is returned by the above function.
The problem is that None doesn't have an encode function (or an encode attribute) and so you get an attribute error that names the type you were trying to get an attribute of and the attribute you were trying to get.

Categories