How to name a function like this? [closed] - python

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
The pseudocode is like this:
def a_func(parent_node, child_node):
parent_node.add(child_node)
// check validity
return child_node
node1 = a_func(pnode, Node(attr_x = "a new node")
node2 = a_func(pnode, Node(node1.get_attr("attr_x"))
It's used in tree structure. a_func insert nodeX into a parent nodeY and return nodeX. Does anyone have ideas about a suitable name for it?

I would name the function something like enlistForParent or maybe addToParent or even setParentForNode I believe all these versions imply that the return value is the child note and also make the effect of calling the function clear.

Related

(Python) Questions about functions and arguments [closed]

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

How to a Python custom function to raised value with inner function [closed]

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

Python: Passing argument from top-level function to embedded function [closed]

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
This is a simplified example. But basically, I have function within a function. I want to pass the argument from the top-level function to the embedded function, but it doesn't work. How do I solve this?
Thank you
def Check(file_name, mh_criticalval):
test_file=file_name
def eval_value(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col) > mh_value:
cell_cny_b=excel_sht.cell(
row=i,
column=excel_col
).value='OK'
eval_value(sht, 9, mh_criticalval)
Check('test.xlsm',1)
Method 1: De-Nesting
The most obvious method here is to remove the nested functions. Doing this is simple:
def eval_mh(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col)>mh_value:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
eval_mh(sht, 9, mh_criticalval)
CheckDiag('test.xlsm',1)
All we did here was move the inner function out. However, if you still wanted them to be nested, try the following.
Method 2: Local/Global Variable Exploitation
Here, we replace all instances of mh_value with mh_criticalval
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
def eval_mh(excel_sht, excel_col):
if excel_sht.cell(row=i, column=excel_col)>mh_criticalval:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)
Method 3: Why 2 functions?
In this method, we remove the function entirely!
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
if sht.cell(row=i, column=9)>mh_criticalval:
cell_cny_b=sht.cell(row=i, column=9).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)

How to call a function only Once in Python [closed]

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

Why my instance does not have a name [closed]

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)
…

Categories

Resources