jupyter is showing callable error in dict function [duplicate] - python

I was using Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2 , when i ran the folllowing code in it the corresponding error is shown .I searched a lot about this but am unable to find why is it so
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable

In a fresh interpreter:
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
>>> bob
{'age': 42, 'pay': '10000', 'job': 'dev', 'name': 'bob smith'}
However, you are getting a TypeError:
TypeError: 'dict' object is not callable
This error you get tells you that your dict is not callable.
Since my dict is callable when I open a fresh interpreter, it means that your dict is different.
Most likely, you defined a dict variable, which overrode the built-in dict. Look for the
dict = {...}
line, and rename your variable.
As pointed out by #Robᵩ, don't use built-in names for your variables. Especially avoid the tempting str, list, and so on.

On a previous line in that interactive session, you have rebound the dict name to some variable. Perhaps you have a line like dict={1:2} or dict=dict(one=1, two=2).
Here is one such session:
>>> dict=dict(one=1)
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
>>>
As a general rule, one should not use built-in type names as variable names, to prevent this error.

edit: Ignore this, I am told this is bad practice.
As mgilson stated, the issue is likely that you have a variable called dict. The solution to this would be to run
del dict
which deletes the variable by that name.

Related

error while creating simple dictionary: 'dict' object is not callable [duplicate]

I was using Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2 , when i ran the folllowing code in it the corresponding error is shown .I searched a lot about this but am unable to find why is it so
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
In a fresh interpreter:
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
>>> bob
{'age': 42, 'pay': '10000', 'job': 'dev', 'name': 'bob smith'}
However, you are getting a TypeError:
TypeError: 'dict' object is not callable
This error you get tells you that your dict is not callable.
Since my dict is callable when I open a fresh interpreter, it means that your dict is different.
Most likely, you defined a dict variable, which overrode the built-in dict. Look for the
dict = {...}
line, and rename your variable.
As pointed out by #Robᵩ, don't use built-in names for your variables. Especially avoid the tempting str, list, and so on.
On a previous line in that interactive session, you have rebound the dict name to some variable. Perhaps you have a line like dict={1:2} or dict=dict(one=1, two=2).
Here is one such session:
>>> dict=dict(one=1)
>>> bob=dict(name='bob smith',age=42,pay='10000',job='dev')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
>>>
As a general rule, one should not use built-in type names as variable names, to prevent this error.
edit: Ignore this, I am told this is bad practice.
As mgilson stated, the issue is likely that you have a variable called dict. The solution to this would be to run
del dict
which deletes the variable by that name.

Python: '{0.lower()}'.format('A') yields 'str' object has no attribute 'lower()'

In Python strings have a method lower():
>>> dir('A')
[... 'ljust', 'lower', 'lstrip', ...]
However, when one tries '{0.lower()}'.format('A'), the response states:
>>> '{0.lower()}'.format('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lower()'
Can someone help me understand why the line above throws an AttributeError in this case? This seems like it should not be an AttributeError, though I must be mistaken. Any help understanding this would be very welcome!
Edit: I understand I can't call the lower() method inside the format call (though it'd be neat if that were possible); my question is why doing so throws an AttributeError. This error seems misleading in this case.
You can't call a method from within a format specification. Dot notation inside the format specifier is a way to look up attribute names and render their values, not to call functions.
0.lower() tries to look up an attribute on the string literally named "lower()" - the equivalent of getattr(some_string, 'lower()'). You need to call the method before formatting.
>>> '{0.lower()}'.format('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lower()'
>>> '{0}'.format('A'.lower())
'a'
As others have said, you can't do this in a format expression. It would work in an f-string though:
a = "A"
print(f"{a.lower()}")

What is the explicit python3 type for dict_keys for isinstance() check?

In Python3, what type should I use to check if the dictionary keys belong to it?
>>> d = {1 : 2}
>>> type(d.keys())
<class 'dict_keys'>
So naturally I tried this:
>>> isinstance(d.keys(), dict_keys)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'dict_keys' is not defined
What should I put in place of the explicit dict_keys as 2nd argument for isinstance?
(This is useful as I have to handle unknown input variables that can take the form of dictionary keys. And I know using list(d.keys()) can convert to a list (recovering Python2 behavior) but that's not an option in this case.)
You can use collections.abc.KeysView:
In [19]: isinstance(d.keys(), collections.abc.KeysView)
Out[19]: True
collections.abc module provides abstract base classes that can be
used to test whether a class provides a particular interface
Using built-in type():
isinstance(d.keys(), type({}.keys()))

type() in python 2.5 returning error when passing in string

Using Python 2.5.2 (r252:60911, Sep 30 2008, 15:42:03)
I try to use
type(x)
and receive the following error when passing in this string '2014-02-06 00:00:00'
TypeError: 'str' object is not callable
why does it not return str?
You probably shadowed the python function type with a string by doing the following:
>>> type = 'some string'
>>> # Lots of code
>>> x = 'other string'
>>> type(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
You can fix this by not using variable names that are also python builtin functions.
If you absolutely cannot rename your variable to be something other than type (_type or type_ will do), you can access the builtin function using the __builtin__ module
>>> type = 'some string'
>>> # Lots of code
>>> x = 'other string'
>>> import __builtin__
>>> __builtin__.type(x)
<type 'str'>
Anyone reading your code will hate you, though.

urllib.request.Request - unexpected keyword argument 'method'

Attempting to use the method argument as seen here yields the following error.
Python 3.2.3 (default, Sep 25 2013, 18:22:43)
>>> import urllib.request as r
>>> r.Request('http://example.com', method='POST')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'method'
>>>
No matter what/where I search, I can't seem to find a solution to my problem.
You're looking at the docs for Python 3.3 but running Python 3.2. In Python 3.2 the Request initializer doesn't have a method argument: http://docs.python.org/3.2/library/urllib.request.html#urllib.request.Request
FWIW depending on what kind of request you make (for example if the request includes a body) urllib will automatically use the appropriate method (i.e. POST). If you need to make a more specialized type of request such as HEAD you need to dig a little deeper. There are other answers on SO that help with that.

Categories

Resources