error in python code while creating an object [closed] - python-3.x

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
An error is coming while declaring an object . Any suggestions would be appreciated.
from tkinter import*
class Bill_App:
def _init_(self,root):
pass
root=Tk()
obj = Bill_App(root)
root.mainloop()
error:-
Traceback (most recent call last):
File "C:\Users\rish6\AppData\Local\Programs\Python\Python37\Bill_App\Bill.py", line 9, in <module>
obj = Bill_App(root)
TypeError: Bill_App() takes no arguments

Error is coming due to wrong syntax of init method.
def _init_(self,root):
it should be __init__ -> def __init__(self,root):
Changed code
from tkinter import*
class Bill_App:
def __init__(self,root):
print('object is created'+str(root))
root=Tk()
obj = Bill_App(root)
root.mainloop()

Related

Unable to import openpyxl module [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The Code below works just fine :
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
import os
os.chdir('c:\\users\\shivam\\Desktop')
wb = openpyxl.load_workbook('data.xlsx')
print(wb.sheetnames)
print(get_column_letter(27))
but when I try to import it as:
from openpyxl import *
import os
os.chdir('c:\\users\\shivam\\Desktop')
wb = openpyxl.load_workbook('data.xlsx')
print(wb.sheetnames)
print(get_column_letter(27))
it gives an error:
Traceback (most recent call last):
File "D:/python projects/excel.py", line 4, in <module>
wb = openpyxl.load_workbook('data.xlsx')
NameError: name 'openpyxl' is not defined
Why everything from the module is not getting imported all at once?
If you import a module like import openpyxl, then you should use openpyxl.load_workbook.
But while importing a module like from openpyxl import *, rather than importing the whole module you're importing all the classes and functions present inside the module, so there is no need of calling openpyxl.something(), just directly call the name of class or function like load_workbook

How to fix 'NameError: name 'field' is not defined' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
In my attempt to create a class dots with the fields n and xy as shown below:
from dataclasses import dataclass
#dataclass
class dots:
n: int = 200
xy: List[int] = field(default_factory=list)
I am constantly getting the error :
NameError: name 'field' is not defined
Any ideas on how to fix it?
My operating system is Ubuntu 18.04.3 LTS, and the kernel version 4.15.0-58-generic. I am using Python 3.6.4
You need to import field() to use it in your code:
from dataclasses import dataclass, field

AttributeError: 'list' object has no attribute 'shape' , how sould i fix this type of error? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
AttributeError: 'list' object has no attribute 'shape'
I am trying to make the code for row and column for given array.
and the top line is my error, how can it be fixed?
def comuteFinalGrades(grades):
a=[]
N, M = grades.shape
You have to convert the list into numpy array and then You can use the shape attribute.
import numpy as np
def comuteFinalGrades(grades):
grades=np.array(grades)
a=[]
N, M = grades.shape

attribute error in python Snake game, what does it mean? [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a class MyThread. In that, I have a method sample. I am trying to run it from within the same object context. Please have a look at the code:
class myThread (threading.Thread):
def __init__(self, threadID, name, counter, redisOpsObj):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
self.redisOpsObj = redisOpsObj
def stop(self):
self.kill_received = True
def sample(self):
print "Hello"
def run(self):
time.sleep(0.1)
print "\n Starting " + self.name
self.sample()
Looks very simple ain't it. But when I run it I get this error
AttributeError: 'myThread' object has no attribute 'sample' Now I have that method, right there. So what's wrong? Please help
Edit: This is the stack trace
Starting Thread-0
Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
I am calling it like this
arThreads = []
maxThreads = 2;
for i in range( maxThreads ):
redisOpsObj = redisOps()
arThreads.append( myThread(i, "Thread-"+str(i), 10, redisOpsObj) )
Sorry, I can't post the redisOps class code. But I can assure you that it works just fine
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.
If you’re using python 3+ this may also occur if you’re using private variables that start with double underscore, e.g., self.__yourvariable. Just something to take note of for some of you who may run into this issue.
These kind of bugs are common when Python multi-threading. What happens is that, on interpreter tear-down, the relevant module (myThread in this case) goes through a sort-of del myThread.
The call self.sample() is roughly equivalent to myThread.__dict__["sample"](self).
But if we're during the interpreter's tear-down sequence, then its own dictionary of known types might've already had myThread deleted, and now it's basically a NoneType - and has no 'sample' attribute.
This may also occur if your using slots in class and have not added this new attribute in slots yet.
class xyz(object):
"""
class description
"""
__slots__ = ['abc', 'ijk']
def __init__(self):
self.abc = 1
self.ijk = 2
self.pqr = 6 # This will throw error 'AttributeError: <name_of_class_object> object has no attribute 'pqr'
I got this error for multi-threading scenario (specifically when dealing with ZMQ). It turned out that socket was still being connected on one thread while another thread already started sending data. The events that occured due to another thread tried to access variables that weren't created yet. If your scenario involves multi-threading and if things work if you add bit of delay then you might have similar issue.
Python protects those members by internally changing the name to include the class name.
You can access such attributes as object._className__attrName.
I have encountered the same error as well. I am sure my indentation did not have any problem. Only restarting the python sell solved the problem.
The same error occurred when I had another variable named mythread. That variable overwrote this and that's why I got error
You can't access outside private fields of a class. private fields are starting with __ .
for example -
class car:
def __init__(self):
self.__updatesoftware()
def drive(self):
print("driving")
def __updatesoftware(self):
print("updating software:")
obj = car()
obj.drive()
obj.__updatesoftware() ## here it will throw an error because
__updatesoftware is an private method.

Python AttributeError when referencing a settings class [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a class MyThread. In that, I have a method sample. I am trying to run it from within the same object context. Please have a look at the code:
class myThread (threading.Thread):
def __init__(self, threadID, name, counter, redisOpsObj):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
self.redisOpsObj = redisOpsObj
def stop(self):
self.kill_received = True
def sample(self):
print "Hello"
def run(self):
time.sleep(0.1)
print "\n Starting " + self.name
self.sample()
Looks very simple ain't it. But when I run it I get this error
AttributeError: 'myThread' object has no attribute 'sample' Now I have that method, right there. So what's wrong? Please help
Edit: This is the stack trace
Starting Thread-0
Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
I am calling it like this
arThreads = []
maxThreads = 2;
for i in range( maxThreads ):
redisOpsObj = redisOps()
arThreads.append( myThread(i, "Thread-"+str(i), 10, redisOpsObj) )
Sorry, I can't post the redisOps class code. But I can assure you that it works just fine
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.
If you’re using python 3+ this may also occur if you’re using private variables that start with double underscore, e.g., self.__yourvariable. Just something to take note of for some of you who may run into this issue.
These kind of bugs are common when Python multi-threading. What happens is that, on interpreter tear-down, the relevant module (myThread in this case) goes through a sort-of del myThread.
The call self.sample() is roughly equivalent to myThread.__dict__["sample"](self).
But if we're during the interpreter's tear-down sequence, then its own dictionary of known types might've already had myThread deleted, and now it's basically a NoneType - and has no 'sample' attribute.
This may also occur if your using slots in class and have not added this new attribute in slots yet.
class xyz(object):
"""
class description
"""
__slots__ = ['abc', 'ijk']
def __init__(self):
self.abc = 1
self.ijk = 2
self.pqr = 6 # This will throw error 'AttributeError: <name_of_class_object> object has no attribute 'pqr'
I got this error for multi-threading scenario (specifically when dealing with ZMQ). It turned out that socket was still being connected on one thread while another thread already started sending data. The events that occured due to another thread tried to access variables that weren't created yet. If your scenario involves multi-threading and if things work if you add bit of delay then you might have similar issue.
Python protects those members by internally changing the name to include the class name.
You can access such attributes as object._className__attrName.
I have encountered the same error as well. I am sure my indentation did not have any problem. Only restarting the python sell solved the problem.
The same error occurred when I had another variable named mythread. That variable overwrote this and that's why I got error
You can't access outside private fields of a class. private fields are starting with __ .
for example -
class car:
def __init__(self):
self.__updatesoftware()
def drive(self):
print("driving")
def __updatesoftware(self):
print("updating software:")
obj = car()
obj.drive()
obj.__updatesoftware() ## here it will throw an error because
__updatesoftware is an private method.

Resources