Global variable in extension

Hi!

I got these extension example. Im passing in a argument squareWidth to the method Size() which I am dividing the global variable resx with.

My question is regarding this global variable.

First I wrote like this but it gave me an error saying that resx is not defined:

[code]class Startup:

resx = 0

def __init__(self):
	resx = op('null1').width
	print(resx)
	return
	
def Size(self, squareWidth):
	print(resx / squareWidth)
	return[/code]

Then I wrote it like this instead which worked:

[code]class Startup:
resx = 0

def __init__(self):
	global resx
	resx = op('null1').width
	print(resx)
	return
	
def Size(self, squareWidth):
	print(resx / squareWidth)
	return[/code]

My question is; in python do I need to redefine a variable as global everytime I change it or are there a better way doing this?
python1.1.toe (3.96 KB)

Hey arcoscene,

Is there a reason you’re using global variables instead of creating a class member?

I usually do something like this:

[code]class Startup:
def init(self):

	self.resx = op('null1').width
	print(self.resx)
	return
	
def Size(self, squareWidth):
	print(self.resx / squareWidth)
	return[/code]

If you were going to use a reference to self.resx in a parameter, you’d just want to make sure you made it a dependency.

Does that help at all?

No there is no reason for that :slight_smile: Actually I wasnt aware of that approach. It might be better approach with encapsulating the class.

Thanks for reply