'NameError' 在新函数中调用现有函数
'NameError' Calling an existing function in a new one
我创建了一个包含多个函数的 class,每当我尝试调用另一个函数中的一个函数时,我都会遇到名称错误的问题。在这种情况下,'Sorting_array' 函数是我调用最多的函数,但它会为每个被调用的函数抛出一个 NameError。下面是 Python 代码:
arr = [5, 1, 9, 7]
#Display initial list
print("The Initial Array is:", arr)
#All array operations in a class
class Allarrays:
def Check_Sort(self):
#Check if the list is sorted or not
global flag #this variable is reusable outside the function
flag = 0
i = 1
while i < len(arr):
if (arr[i] < arr[i - 1]):
flag = 1
i+=1
if (not flag):
print("This array is sorted.")
else:
print("This array is unsorted, it needs to be sorted")
def Sorting_array(self, arr):
#sort unsorted list by swapping positions
for i in range (len(arr)):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[I]
return arr
def Not_Sorted(self):
if flag:
#This only displays this if the list is unsorted
Sorting_array(arr) #Calling existing function for sorting array
print("The sorted array is:", arr) #Display result
def missing_element(self):
global missing_numbers #this variable is reusable outside the function
#Check for missing numbers in the list
missing_numbers = [item for item in range(arr[0], arr[-1]+1)
if item not in arr]
print("The following numbers are the missing elements:", missing_numbers)
return arr
def Concatenate_arrays(self):
global m #this variable is reusable outside the function
#Merging missing numbers to existing list
m = arr + missing_numbers #List Concatenation
Sorting_array(m) #Calling existing function
#for sorting list with a new parameter
print("The Complete array with replaced missing elements", m) #Display result
return m
def Rvsal(self, m):
#Reversing elements of the list by swapping the positions
revs = m[::-1] #Swap positions of the elements
print("The reversed array", revs) #Display resulted array
def nth_highest():
#Checking the list for the second-highest number
Sorting_array(m) #Sorting the list with an existing funtion and argument
Second_highest = m[-2] #Assigning the second to the last element after sorting
print("The second highest number of the array is:", Second_highest)
#Displays result
def Sum_Positive(self):
#Sum of all positive numbers in the list
pos = sum(x for x in m if x > 0) #Sum numbers that are greater than zero
print("The sum of all positive number in the complete array is:", pos)
#Displays result
def Sum_Negative(self):
#Sum of all Negative numbers in the list
neg = sum(x for x in m if x < 0) #Sum numbers that are less than zero
if neg is 0:
print("There are no negative numbers in the complete array") #To display
#if there are no negative numbers
else:
print("The sum of all negative number in the complete array is:", neg)
#Displays result
#Creating an object for the class Allarrays
ca = Allarrays()
#Calling the functions through the class
ca.Check_Sort()
ca.Sorting_array(arr)
ca.Not_Sorted()
ca.missing_element()
ca.Concatenate_arrays()
ca.Rvsal(m)
ca.nth_highest()
ca.Sum_Positive()
ca.Sum_Negative()
在整个class中,Sorting_array函数在定义后调用了三次,一直抛出NameError,求助。谢谢!
由于您是从同一个 class 调用一个函数,因此您必须在调用它时使用 self.Sorting_array()
。
顺便说一下,根据 PEP8 标准,您应该尽量将函数命名为小写。
我创建了一个包含多个函数的 class,每当我尝试调用另一个函数中的一个函数时,我都会遇到名称错误的问题。在这种情况下,'Sorting_array' 函数是我调用最多的函数,但它会为每个被调用的函数抛出一个 NameError。下面是 Python 代码:
arr = [5, 1, 9, 7]
#Display initial list
print("The Initial Array is:", arr)
#All array operations in a class
class Allarrays:
def Check_Sort(self):
#Check if the list is sorted or not
global flag #this variable is reusable outside the function
flag = 0
i = 1
while i < len(arr):
if (arr[i] < arr[i - 1]):
flag = 1
i+=1
if (not flag):
print("This array is sorted.")
else:
print("This array is unsorted, it needs to be sorted")
def Sorting_array(self, arr):
#sort unsorted list by swapping positions
for i in range (len(arr)):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[I]
return arr
def Not_Sorted(self):
if flag:
#This only displays this if the list is unsorted
Sorting_array(arr) #Calling existing function for sorting array
print("The sorted array is:", arr) #Display result
def missing_element(self):
global missing_numbers #this variable is reusable outside the function
#Check for missing numbers in the list
missing_numbers = [item for item in range(arr[0], arr[-1]+1)
if item not in arr]
print("The following numbers are the missing elements:", missing_numbers)
return arr
def Concatenate_arrays(self):
global m #this variable is reusable outside the function
#Merging missing numbers to existing list
m = arr + missing_numbers #List Concatenation
Sorting_array(m) #Calling existing function
#for sorting list with a new parameter
print("The Complete array with replaced missing elements", m) #Display result
return m
def Rvsal(self, m):
#Reversing elements of the list by swapping the positions
revs = m[::-1] #Swap positions of the elements
print("The reversed array", revs) #Display resulted array
def nth_highest():
#Checking the list for the second-highest number
Sorting_array(m) #Sorting the list with an existing funtion and argument
Second_highest = m[-2] #Assigning the second to the last element after sorting
print("The second highest number of the array is:", Second_highest)
#Displays result
def Sum_Positive(self):
#Sum of all positive numbers in the list
pos = sum(x for x in m if x > 0) #Sum numbers that are greater than zero
print("The sum of all positive number in the complete array is:", pos)
#Displays result
def Sum_Negative(self):
#Sum of all Negative numbers in the list
neg = sum(x for x in m if x < 0) #Sum numbers that are less than zero
if neg is 0:
print("There are no negative numbers in the complete array") #To display
#if there are no negative numbers
else:
print("The sum of all negative number in the complete array is:", neg)
#Displays result
#Creating an object for the class Allarrays
ca = Allarrays()
#Calling the functions through the class
ca.Check_Sort()
ca.Sorting_array(arr)
ca.Not_Sorted()
ca.missing_element()
ca.Concatenate_arrays()
ca.Rvsal(m)
ca.nth_highest()
ca.Sum_Positive()
ca.Sum_Negative()
在整个class中,Sorting_array函数在定义后调用了三次,一直抛出NameError,求助。谢谢!
由于您是从同一个 class 调用一个函数,因此您必须在调用它时使用 self.Sorting_array()
。
顺便说一下,根据 PEP8 标准,您应该尽量将函数命名为小写。