I’m learning Python OOP and trying to convert a Java class to a Python class
See page 15 in this PDF for Java code google doc link
class QuickFindUF:
"""docstring for QuickFindUF"""
def __init__(self, n):
self.id = []
for e in range(n):
self.id.append(e)
def connected(self,p,q):
return self.id[p]==self.id[q]
def union(self,p,q):
self.pid = self.id[p]
self.qid = self.id[q]
for i in range(len(self.id)):
if(self.id[i]==self.pid):
self.id[i]=self.qid
quf = QuickFindUF(9)
quf.union(3,4)
print quf.connected(3,4)
There are 16 self keywords in this class. Is there a better way to write this class?
Yea, you don’t want to assign these variables to
self, these are local variables:Should be:
You only use
selfwhen referring to instance variables, not to any variable inside of a method.