Why does the following attribute error occur? - python

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)))

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'

Python Try/Except firing TypeError despite Except clause

I'm following along with the Python docs for try/except with the following effort:
def profile_check(self):
try:
profile = self.profile_type
return profile
except TypeError:
...
self.profile_type is a field in a Django model which doesn't exist in this case (therefore it returns as None). However, something seems to be missing, because it never moves on to the actions for except, rather it immediately throws the TypeError exception:
>>> a.profile_check()
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'NoneType' object is not callable
This is my first effort with try-catch, so I know it's something basic.
First off, your problem is that profile_check is set to None for some reason. That's why it's giving you that error when you call it.
As for your try/catch problem, if a variable doesn't exist, then the Python interpreter will throw a NameError at you. Try substituting TypeError for that.
I can't help you with your profile_check-is-None situation though without anymore context though. Sorry!

AttributeError, why?

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.

Python "NoneType is not callable" error

I have a function that looks like the following, with a whole lot of optional parameters. One of these parameters, somewhere amidst all the others, is text.
I handle text specially because if it is a boolean, then I want to run to do something based on that. If it's not (which means it's just a string), then I do something else. The code looks roughly like this:
def foo(self, arg1=None, arg2=None, arg3=None, ..., text=None, argN=None, ...):
...
if text is not None:
if type(text)==bool:
if text:
# Do something
else:
# Do something else
else:
# Do something else
I get the following error on the type(text)==bool line:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...", line 79, in foo
if type(text)==bool:
TypeError: 'NoneType' object is not callable
Not sure what the problem is. Should I be testing the type differently? Experimenting on the python command line seems to confirm that my way of doing it should work.
I guess you have an argument called type somewhere, I can easily reproduce your error with the following code:
>>> type('abc')
<class 'str'>
>>> type = None
>>> type('abc')
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
type('abc')
TypeError: 'NoneType' object is not callable
I bet you have a type=None among your arguments.
Just a special case of the general rule: "don't hide built-in identifiers with your own -- it may or may not bite in any specific give case, but it will bite you nastily in some cases in the future unless you develop the right habit about it"!-)

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