I don't exactly know how I whiffed this, but at some point I input
repr = 64
into the python console in spyder. When I now try to run repr(b64) this happens:
repr(b64)
Traceback (most recent call last):
File "<ipython-input-23-8c64b01419a6>", line 1, in <module>
repr(b64)
TypeError: 'int' object is not callable
can I fix this without restarting spyder?
Delete your variable:
del repr
This will clear the binding you created, unhiding the builtin. (It will not remove the builtin repr.) This gets you back to a slightly cleaner state than repr = builtins.repr would, though it usually won't matter.
Use builtins.repr:
>>> repr = 42
>>> repr(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> from builtins import repr
>>> repr(42)
'42'
(or use del as suggested by user2357112).
Related
This question already has answers here:
Python typing for module type
(1 answer)
Check if a parameter is a Python module?
(6 answers)
Closed 2 years ago.
If I import a module in Python, then the type of the module is, unsurprisingly, module. However, I'm not sure sure how to reference this type directly. Here by "directly" I mean by writing something like module rather than taking the type of a module. For example, in the following shell interaction, I can test whether an object obj is an int by writing isinstance(obj, int), but evidently I cannot test whether obj is a module by writing isinstance(obj, module).
>>> import random
>>> type(random)
<class 'module'>
>>> isinstance(random, module)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'module' is not defined
>>> isinstance(random, 'module')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type or tuple of types
>>> isinstance(random, type(random))
True
>>> isinstance(1, int)
True
>>> isinstance('Hello, World!', str)
True
This question already has answers here:
How int() object uses "==" operator without __eq__() method in python2?
(3 answers)
Closed 2 years ago.
I've got a head scratchier and it seems I'm not the only one, but is there really no solution? I find that hard to believe!
So the question is why can't I call int.__eq__ with 2 operators or i.__eq__ with one? How can I use __eq__ (and the other comparison operators) for a per item comparison for a sequence of ints?
Here's a dump from python2.7.17:
>>> i = 0
>>> type(i)
<type 'int'>
>>> i.__eq__(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__eq__'
>>> type(i).__eq__(i, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 2
>>> type(i).__eq__(0)
NotImplemented
But my dumo from python3.6.9 behaves itself:
>>> i = 0
>>> type(i)
<class 'int'>
>>> i.__eq__(0)
True
>>> type(i).__eq__(i, 0)
True
>>> type(i).__eq__(0) # this is not expected to work, but just for the sake of voodoo.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 0
I know python2 is no longer supported but there are a few applications that only use python2 and I would like to make my code backwards compatible anyway.
So anyone out there have a solutuon for hacking the comparison magic operator methods as function calls in python2? I am sure there must be some work around.
It seems there is some information on this. I just read that python2 falls back to using cmp in some cases, while in python3 there is no cmp (or so I read). So I guess the thing to do is not use eq and ne but instead use cmp but I love some additional perspective on this
The general rule is: don't touch dunderscore methods, use functions and operators instead and those will delegate to dunderscore implementation as necessary. In your case you're looking for the == operator or the functional equivalent operator.eq.
from operator import eq
from functools import partial
print eq(1, 2)
f = partial(eq, 1)
print f(2)
I am starting to play around with python a little, and as a novice i tried this:
>>> s="";str=""
>>> for x in [ 1,2,3,4,5,6 ] :
... s += str(x)
...
Traceback (most recent call last):
File "<console>", line 3, in <module>
TypeError: 'str' object is not callable
I accidentally declared a variable called str (str is supposed to be a function).
Assuming it would break the semantics too much, even in a dynamically
typed language, is there a namespace i can use to qualify methods
like str and ensure this does not happen or make it difficult?
This is what import <module> instead of from <module> import * is used for. As long as you use str in the only meaning of local variable value in <module>, you can use
module.str elswhere, without mangling namespace.
The only tokens that can't be clashed are keywords. This is intended functionality and there is no way to prevent this: everything is an object in Python
You might want to use some IDE tools, p.ex. Eclipse+PyDev, that checks your code and warn for possible errors.
As per your question you have already defined str=""
so when you will call str method which converts values into string it will not call actual method in place of that it will call str="".
that's why you are getting error because you can not call a str object to convert int to string.
I had an interesting (potentially stupid) idea: What happens if I use a built-in function name as a variable to assign some object (say integer). Here's what I tried:
>>> a = [1,2,3,4]
>>> len(a)
4
>>> len = 1
>>> len(a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'int' object is not callable
Seems like python does not treat function and variable names differently. Without restarting the python interpreter, is there a way to assign len back to the function? Or undo the assignment len = 1?
Use del len:
>>> a=[1,2,3,4]
>>> len=15
>>> len(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del len
>>> len(a)
4
From docs.python.org:
Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. If the name is unbound, a NameError exception will be raised
Technically you can get it back from __builtin__
from __builtin__ import len
But please don't name stuff len, it makes sensible programmers angry.
Okay, for a start don't name your variable after the builtins, secondly if you want to respect other functions then respect namespaces for example
import time
time.asctime()
asctime = 4253
time.asctime() # Notice that asctime here is unaffected as its inside the time module(s) namespace
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"!-)