recursion within a class

2024/9/18 20:26:39

I am trying to place a recursive formula inside a class statement

class SomeNode:def __init__(self, a):leng = len(a)half= leng/2self.firstnode=a[0][0]self.child1=SomeNode([a[i]for k in range(leng)])self.child2=SomeNode([a[j] for j in range(leng)])def recursfunc(self):print self.firstnoderecursfunc(self.child1)recursfunc(self.child2)

However I keep getting the error message NameError: global name 'recursfunc' is not defined

Answer

You need to use self.recursfunc()

https://en.xdnf.cn/q/72222.html

Related Q&A

There is an example of Spyne client?

Im trying to use spyne (http://spyne.io) in my server with ZeroMQ and MsgPack. Ive followed the examples to program the server side, but i cant find any example that helps me to know how to program the…

Safely bind method from one class to another class in Python [duplicate]

This question already has answers here:What is the difference between a function, an unbound method and a bound method?(6 answers)Closed 5 years ago.I know I can attach a function to a class and make …

Basic Python OpenCV cropping and resizing

can someone help me with a little cropping algorithm? its openCV.. im trying to figure this out. I know the method is crop = image[y:y1, x:x1]. If I have an image with new_dimensionXxnew_dimensionY pi…

Why does Keras loss drop dramatically after the first epoch?

Im training a U-Net CNN in Keras/Tensorflow and find that loss massively decreases between the last batch of the first epoch, and the first batch of the second epoch: Epoch 00001: loss improved from in…

extract strings from a binary file in python

I have a project where I am given a file and i need to extract the strings from the file. Basically think of the "strings" command in linux but im doing this in python. The next condition is …

Installing numpy on Mac to work on AWS Lambda

Is there a way to install numpy on a Mac so that it will work when uploaded to AWS Lambda? I have tried a variety of different ways, including using different pip versions, using easy_install, and fol…

python- how to get the output of the function used in Timer

I want to run a function for 10s then do other stuff. This is my code using Timerfrom threading import Timer import timedef timeout():b=truereturn ba=false t = Timer(10,timeout) t.start()while(a==f…

Create automated tests for interactive shell based on Pythons cmd module

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. Id like to c…

Matplotlib with multiprocessing freeze computer

I have an issue with matplotlib and multiprocessing. I launch a first process, where I display an image and select an area, and close the figure. Then I launch another process, where I call a graph fun…

Pull Tag Value using BeautifulSoup

Can someone direct me as how to pull the value of a tag using BeautifulSoup? I read the documentation but had a hard time navigating through it. For example, if I had:<span title="Funstuff&qu…