Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The snippet of code is below. I want my object name to be equal to element[0]. My scripts successfully generates the objects, but fails to 'name them' I cannot understand why.
master_inventory = []
def import_catalogue():
with open("./catalogue.txt", "r") as raw_catalogue:
for page in raw_catalogue:
page = page.split('\r')
for line in page:
element = line.split('\t\t\t')
element[0] = Antique(element[0], element[1], element[2], element[3], element[4], element[5], element[6], element[7], element[8], element[9])
master_inventory.append(element[0])
import_catalogue()
print master_inventory[1]
>>> <__main__.Antique instance at 0x10980f320>
print master_inventory[1].sku
>>> A00001
You should implement the __str__ function in the Antique class, so you choose how you want to display your object's name. e.g.:
class Antique():
…
def __str__(self):
return "Antique({},{},{})".format(self.foo, self.bar, self.foobar)
…
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I have code:
def users_list_reaction(users_dict, prefix_dictionary, event):
login = event.widget.get()
prefix_str = users_dict.get(f'{login}')
for i in prefix_str[0]:
if i == ' ':
pass
else:
for x in i:
prefix_dictionary[x].select()
But I only need to pass two of the three arguments. I have error TypeError: users_list_reaction() missing 1 required keyword-only argument: 'event'. None how answer will not help, because the event is a tkinter library widget and it is needed to get information from the combobox. I don't want use global users_dict . What to do?
I tried to search for information on the Internet, but I don’t understand how to formulate it correctly, because I didn’t find an answer to mine
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
This is the current code, tried to print but it return None..can some1 enlightened me how to do it?
def calculate(Amount1, Amount2):
def inner(square,cube):
square=Amount1**2
cube=Amount2**3
return (inner(Amount1,Amount2))
print(calculate(2,4))
Expected result (4,64)
def calculate(Amount1, Amount2):
def inner(square,cube):
square=Amount1**2
cube=Amount2**3
return((square, cube))
return (inner(Amount1,Amount2))
print(calculate(2,4))
you forgot to return the values from your inner function
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Define a function one() that prints the string "Question 1". Do NOT call the function.
and the result should be
TEST : one()
Result: Question 1
I have tried this code
def one():
print ("Question 1")
but nothing happened
I believe you just need to remove the space after "print" to get what you are trying to do, but as #Brack mentions, you will still need to call the function:
def one():
print('Question 1')
Console Input:
one()
Output:
'Question 1'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
error:
UnboundLocalError: local variable 'x' referenced before assignment
x is not defined yet...
can i disable x in buttonAritmethic and only enable when I call him?
buttonAritmethic = (x*imgfW)+buttonCenter, 350
for x in range(buttonReach):
self.canvasbackground = self.canvasFrame.create_image(buttonAritmethic, anchor = "nw", image = self.imgf)
Why don't you make it into a function?
def buttonAritmethic(x):
return (x*imgfW)+buttonCenter, 350
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
here I want to call web service function only once throughout the program.
how to accomplish this anybody suggest me
import sys,os
def web_service(macid):
# do something
if "__name__" = "__main__" :
web_service(macid)
This is how I would to that:
i_run_once_has_been_run = False
def i_run_once(macid):
global i_run_once_has_been_run
if i_run_once_has_been_run:
return
# do something
i_run_once_has_been_run = True
#Vaulstein's decorator function would work too, and may even be a bit more pythonic - but it seems like a bit overkill to me.
Using class,
class CallOnce(object):
called = False
def web_service(cls, macid):
if cls.called:
print "already called"
return
else:
# do stuff
print "called once"
cls.called = True
return
macid = "123"
call_once_object = CallOnce()
call_once_object.web_service(macid)
call_once_object.web_service(macid)
call_once_object.web_service(macid)
Result is,
I have no name!#sla-334:~/stack_o$ python once.py
called once
already called
already called