This is pretty simple:
import threading as t
t.local().x = 1
print t.local().x
When I run it, I get this:
Traceback (most recent call last):
File "C:\Documents and Settings\e272nk\Desktop\tst.py", line 3, in <module>
print t.local().x
AttributeError: 'thread._local' object has no attribute 'x'
The attribute assignment seems to be working ok, why can't I retrieve the value?
This is Python v2.7.5 on Windows XP.
You're creating an instance of the class local, setting an attribute on it, then discarding it. Then in the next line, you are creating another instance of local and trying to print a certain attribute from it. Since you didn't set the attribute on that instance, it doesn't have it, and you get an error.
As shown in the documentation, you should be creating an instance of local and keeping it around:
import threading as t
locs = t.local()
locs.x = 1
print locs.x
Related
I'm new to programming and I can't able to run this code it often shows an error like module object is not callable. Can anyone sort this out for me?
import simplekml
kml = simplekml.kml() #what's wrong here?
kml.newpoit(name="sample",coords[(11,12)])
kml.save("H:\\python\\point.kml")`
Traceback (most recent call last):
File "H:/programs/practice.py", line 2, in <module>
kml = simplekml.kml()
TypeError: 'module' object is not callable
I guess you meant to call simplekml.Kml() (with capital K) to create an instance of this class. So probably it was just misspelling.
simplekml is a module, which you import in the very first line of your code.
I advise you to have a look at simplekml.Kml class documentation.
simplekml.kml is a module inside simplekml, and thus isn't callable, as the error message says. You probably meant to call simplekml.Kml() (with a capital K), to create a Kml instance.
This seems like a simple issue but I'm having a very difficult time understanding why I am getting the following error:
Traceback (most recent call last):
File "....py", line 46, in update
self.Grob3Text.SetLabel('Grob 3: ' + str(Grob3))
AttributeError: 'Frame' object has no attribute 'Grob3Text'
When running the following script:
Please see -
.
I understand the message is telling me that the static text variable 'Grob3Text' is not defined in the Frame, but it appears to me that the definition for update event is under the same class. All I'm trying to do is execute the faultreport function and update the static text. I have got this working in another script but for whatever reason I am overlooking something here.
You create a local variable Grob3Text in __init__, but you don't actually store it as an instance attribute on the class instance. To do that, you could simply do:
self.Grob3Text = Grob3Text
in __init__ after you create Grob3Text.
I've got file named recommend.py. It has a dict data named critics.
When I try to reload it in the interpreter it gives the following error:
>>> from recommend import critics
>>> reload(recommend.py)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'recommend' is not defined
>>>
I'm new to python. Please help me.
recommend.py is parsed as recommend . py which means that python looks for an object bound to the name recommend and then tries to get the py attribute from it. That doesn't work because you don't have an object named recommend in the current namespace and because even if you did have an object bound to that name, it probably wouldn't have an attribute py.
Of course, you'll need to give reload an actual module object. Something more like:
import recommend
reload(recommend)
reload() takes a module object, not a filename:
import recommend
reload(recommend)
I have an object in the heap and a reference to it. There are certain circumstances in which the object gets deleted but the reference that points to its location doesn't know that. How can I check if there is real data in the heap?
For example:
from PySide import *
a = QProgressBar()
b = QProgressBar()
self.setIndexWidget(index,a)
self.setIndexWidget(index,b)
Then the a object gets deleted but print(a) returns a valid address. However if you try a.value() - runtime error occurs (C++ object already deleted).
a is None returns False.
For the PySide objects you'll need the shiboken module to perform object queries. For Pyside2, you'll need shiboken2.
import shiboken # shiboken2
print shiboken.isValid(a)
use sip module, read more about sip here
import sip
a = QProgressBar()
sip.isdeleted(a)
False
sip.delete(a)
a
<PyQt4.QtCore.QObject object at 0x017CCA98>
sip.isdeleted(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: underlying C/C++ object has been deleted
It is explicitly mentioned in the documentation when an object takes the responsibility for the deletion of another object. In your example, you can see this in the Qt doc :
If index widget A is replaced with index widget B, index widget A will be deleted.
class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)
I created a file "abc.py" and put above in it.
>>> import abc
>>> d = abc.UserDict()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'UserDict'
Most certainly you are importing the Python abc module for abstract base classes instead of your own abc.py. Better choose a different name for your module.
Edit: Of course it is possible to have your own module with the same name as a built-in module and to import it. You have to make sure that your module is in the interpreter's working directory or set the Python path correctly. But it is much easier to avoid the name clash -- in particular in this case, where you presumably don't care about the module's name anyway.