Accessing object from different class Python [closed] - python

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 1 year ago.
Improve this question
I'm new to python, and I'm using PyCharm. I met a problem when trying to access a list in an object.
I have a file to store the list object, let's say Box.py, and a main file to run the programme, Main.py.
And this is what is inside my Box.py:
class Container:
def __int__(self):
self.list = ["1", "2", "3"]
And now I want to print the list in the class Container from my Main.py.
This is what I wrote for my Main.py:
from MainProgramme import Container
def main():
container = Box.Container()
print(container.list)
if __name__ == "__main__":
main()
So, I have created an object container, and use it to access the list in the object.
But when I run the Main.py, there was an error saying:
Traceback (most recent call last):
File "D:\PyCharm\MyProject\MainProgramme\Main.py", line 8, in <module>
main()
File "D:\PyCharm\MyProject\MainProgramme\Main.py", line 5, in main
print(container.list)
AttributeError: 'Container' object has no attribute 'list'
May I know what is the problem, and how do I solve it?

You have a syntax error, you aren't actually calling init.
def __int__(self):
should be
def __init__(self):
Also, to run your code, I had to change the creation of the Container object.
container = Box.Container()

Related

can someone tell me why I'm getting this error? [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 2 years ago.
Improve this question
class Solution:
def remove(self,arr,target):
if target in arr:
arr.remove(target)
remove(arr,target)
return len(arr),arr
else:
return "not in the array"
ans=Solution()
print(ans.remove([3,2,2,3],3))
This is the Error
Traceback (most recent call last):
File "c:\Users\ashut\Practice\scrap.py", line 10, in <module>
print(ans.remove([3,2,2,3],3))
File "c:\Users\ashut\Practice\scrap.py", line 5, in remove
remove(arr,target)
NameError: name 'remove' is not defined
Somehow the above program runs in google colab and I've tried restarting the runtime
You have to add the "self" tag before calling the function inside ur function.
class Solution:
def remove(self,arr,target):
if target in arr:
arr.remove(target)
self.remove(arr,target)
return len(arr),arr
else:
return "not in the array"
ans=Solution()
print(ans.remove([3,2,2,3],3))
The temp variable name is arr, is that mean Array?
If so, the code "remove(arr, target)" is no need, remove this line, is OK.

The function does not run when I call it, Python [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 2 years ago.
Improve this question
I want to call a function (my_function), where clearly in my code I am calling it but if the function is not executing.
Someone knows what this is??
Here is my code:
import pandas as pd
my_variable = "1"
if my_variable == "1":
my_function()
global my_function
def my_function():
def tryloc(df, col, idx, default=None):
try:
return df.iloc[col, idx]
except IndexError:
return default
print("hello")
edit = pd.read_csv("priv/productos.csv")
product = tryloc(edit, 1, 0)
print(product)
and the second problem is that it says that this function does not exist when I am declaring it globally
Thanks if you answer!
Despite popular belief, "it's not working" isn't adequate information to diagnose a problem. That said, you are calling the function before it is even declare. You must declare a function before it can be called, always.Try moving
myFunction();
after where you defined it. But, there is a lot more going on here that's wrong and the question doesn't explain what the desired outcome is so I'm not sure how to help. I recommend going back and checking out some python scope documents.

Python3 - TypeException take 1 argument 2 where given [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 4 years ago.
Improve this question
The program i am working on have a class with constructor defined as follow :
def Oracle(object) :
Agold = None
sentence = None
def __init__(self, sentence, Agold):
self.Agold = Agold
self.sentence = sentence
but, when i call the constructor in my main method, as follow :
oracle = Oracle(words, ref_tree)
python 3 give me this error :
Traceback (most recent call last):
File "oracle_test.py", line 52, in test_exemple
oracle = Oracle(words, ref_tree)
TypeError: Oracle() takes 1 positional argument but 2 were given
i don't understand the origin of this problem, and i don't see what gone wrong.
Can someone give me an explanation ?
Thanks
You defined Oracle as a function instead of a class. Use class instead of def. Also, assuming Agold and sentence are supposed to be instance variables instead of class variables, Agold = None and sentence = None are not needed (see this).
class Oracle(object):
def __init__(self, sentence, Agold):
self.Agold = Agold
self.sentence = sentence

'builtin_function_or_method' object has no attribute 'execute' for cursor.ececute(statement) [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 5 years ago.
Improve this question
c = sqlite3.connect(history_db)
cursor = c.cursor
select_statement = "SELECT urls.urls,urls.Visit_count FROM urls,Visits WHERE
urls.id=visits.urls;"
cursor.execute(select_statement)
results = c.cursor.fetchcall()
print(results)
The code above when executed gives an error something like
Traceback (most recent call last):
File "test.py", line 13, in <module>
cursor.execute(select_statement)
AttributeError: 'builtin_function_or_method' object has no attribute
'execute'
I am new to using python sqlite3 so how do I execute this query with sqlite3 in python?
Connection.cursor is a method, if you don't call it you get the method object itself, not the result of calling it. IOW, what you want is
cursor = c.cursor()

Constructor return me None type instead of class type [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 6 years ago.
Improve this question
I've a problem with python 3, classes and contructor. I've my class:
class Menu:
def __init__(self, store):
self.store = store
# other code
and when I create a object
menu = Menu(store)
the variable menu is None type, instead of being Menu type.
Can anyone help me, please?
Psychic debugging: Assuming you haven't replaced Menu somewhere else with something completely different, you defined a __new__ on Menu in your # other code, and you failed to return a newly created object from __new__; thus, __new__ returns None, and you don't actually construct anything.
Typically, you don't need both __new__ and __init__ on user defined classes in Python; just do all the initialization work in __init__ and get rid of __new__.

Categories