This question already has answers here:
Python: function and variable with the same name
(3 answers)
Closed 6 years ago.
I'm newbie in python.
As I learned, str(123) returns the string format of value 123 which is '123'
but what if we have a variable named str, how can I call the str function?
In[2]: str(123)
Out[2]: '123'
In[3]: str='hello world'
In[4]: str(123)
Traceback (most recent call last):
File "/Users/x625/anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-6d97c31da288>", line 1, in <module>
str(123)
TypeError: 'str' object is not callable
How can I call str() function again?
You still can access it:
import builtins
builtins.str(123)
Don't use str as a variable name, it is a keyword.
Just don't call your variables str.
If you made a mistake in interactive mode and need to get rid of a variable you picked a bad name for, you can do del str, but don't do that in an actual program.
Your variable name str='hello world' collides withe the function str.
Just pick another name to your variable.
Related
This question already has answers here:
Recommended way/place to create index on MongoDB collection for a web application
(3 answers)
Closed 4 years ago.
I want to enable text-search at a specific field in my Mongo DB. I want to implement this search in python (-> pymongo). When I follow the instructions given in the internet:
db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index")
I get the following error message:
Traceback (most recent call last):
File "CVE_search.py", line 8, in <module>
db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index")
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1599, in ensure_index
return self.create_index(key_or_list, cache_for, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1466, in create_index
index_doc = helpers._index_document(keys)
File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 100, in _index_document
for (key, value) in index_list:
ValueError: too many values to unpack
Is there a different/better way to create an index in pymongo?
Use the create_index method where you pass in the keys as an array and TEXT as the index direction :
collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')
how to resolve 'int' object is not callable
I am new over here
-68+(((68)**2-4(34)(-510))**0.5)/(2*34)
Traceback (most recent call last):
File "", line 1, in
-68+(((68)**2-4(34)(-510))**0.5)/(2*34)
TypeError: 'int' object is not callable
there is no output only the error message written above. How to resolve this error message?
Python does not support multiplication through parentheses (like (34)(-510) and 4(34)). Change this to (34) * (-510). So, your full line would be:
>>> -68+(((68)**2-4*(34)*(-510))**0.5)/(2*34)
-64.0
When you say 4(34), you're actually telling the interpreter to call the function named 4 with the argument 34. (This is the same syntax as saying a(34), where a is a function.) The error is because 4 is not a function, so you can't call it.
If you don't work in the python interpreter directly, you need to do something with your calcul, like putting it in a variable like this :
a = -68+(((68)**2-4*(34)*(-510))**0.5)/(2*34)
or print it :
print(-68+(((68)**2-4*(34)*(-510))**0.5)/(2*34))
Also you can't do maths with two values into parenthesis, you need to include a '*'.
Finally, doing 4(34) is like you're calling a function named "4", with an argument (34).
I'm having trouble wrapping my brain around this error. It's really basic, but it seems to say the opposite of what is true.
>>> x=b'hi'
>>> urllib.parse.unquote(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 609, in unquote
if '%' not in string:
TypeError: a bytes-like object is required, not 'str'
I've passed the code a bytes object, I would hope that's bytes-like enough. urllib.parse.unquote() seems to only work with a str object.
So why would it generate an error saying it needs a bytes-like object?
I suggest to use urllib.parse.unquote_to_bytes, which encodes its string parameter to bytes if it receives a str object.
urllib.parse.unquote_to_bytes(x)
I am fairly new to python so mostly everything I've tried so far has failed.
Whenever I run this snip of my code to try to make a single string from a sequence,
with open('C:/Users/Arda Turan/Desktop/sequence.txt') as file:
lines = []
for line in file:
line+=(line.rstrip().split(","))
It gives the following error:
"C:\Users\Arda Turan\PycharmProjects\Untitled\venv\Scripts\python.exe"
"C:/Users/Arda Turan/.PyCharmCE2018.1/config/scratches/scratch_1.py"
Traceback (most recent call last):
File "C:/Users/Arda Turan/.PyCharmCE2018.1/config/scratches/scratch_1.py",
line 4, in <module>
line+=(line.rstrip().split(","))
TypeError: must be str, not list
Process finished with exit code 1
Any help would be appreciated.
I suppose, you meant:
for line in file:
lines.append(line.rstrip().split(","))
yes: your lines is an array, try with lines.append()
you're now using "lines" as if it is a string, while you have to use it as array, so use lines.append() instead of +=
if you want to do a "longer" string, change the declaration of lines with lines = '' and use the string catenation += so you'll have a longer string instead of an array of strings
You need to use list.append to append an element into the list. Try the following:
lines = []
with open('C:/Users/Arda Turan/Desktop/sequence.txt') as file:
for line in file:
lines.append(line.rstrip().split(","))
I originally put this code through Python 2.7 but needed to move to Python 3.x because of work. I've been trying to figure out how to get this code to work in Python 3.2, with no luck.
import subprocess
cmd = subprocess.Popen('net use', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if 'no' in line:
print (line)
I get this error
if 'no' in (line):
TypeError: Type str doesn't support the buffer API
Can anyone provide me with an answer as to why this is and/or some documentation to read?
Much appreciated.
Python 3 uses the bytes type in a lot places where the encoding is not clearly defined. The stdout of your subprocess is a file object working with bytes data. So, you cannot check if there is some string within a bytes object, e.g.:
>>> 'no' in b'some bytes string'
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
'no' in b'some bytes string'
TypeError: Type str doesn't support the buffer API
What you need to do instead is a test if the bytes string contains another bytes string:
>>> b'no' in b'some bytes string'
False
So, back to your problem, this should work:
if b'no' in line:
print(line)