0

Win 10, x64, Python 2.7.8

I'm trying to impliment the following (this is a stripped down version of the full code but replicates the problem)

class TrabGraph:

    def __init__(self):            
        self.radius = 1.0

    def circle(self, rad=self.radius):
        print rad

test = TrabGraph()
test.circle(rad=2.0)

I get the following error...

Traceback (most recent call last): File "D:/Lattice/variableTest.FCMacro", line 6, in class TrabGraph: File "D:/Lattice/variableTest.FCMacro", line 12, in TrabGraph def circle(self, rad=self.radius): : name 'self' is not defined

So I tried...

class TrabGraph:

    def __init__(self):            
        self.radius = 1.0

    def circle(self, rad=radius):
        print rad

test = TrabGraph()
test.circle(rad=2.0)

and got the following error...

Traceback (most recent call last): File "D:/Lattice/variableTest.FCMacro", line 6, in class TrabGraph: File "D:/Lattice/variableTest.FCMacro", line 12, in TrabGraph def circle(self, rad=radius): : name 'radius' is not defined

So what is it that I'm not understanding about how objects work? How can I have the default value for rad be the same as self.radius but still be able to change it when I call the circle method if needed?

DrBwts
  • 3,470
  • 6
  • 38
  • 62

1 Answers1

0

try this

class TrabGraph:
    def circle(self):
        print (self.radius)
    def __init__(self,radius):            
        self.radius = radius

TrabGraph(2).circle()
kanav anand
  • 411
  • 1
  • 4
  • 14