Python Forum

Full Version: can't pass instance variables between functions?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi !
I always thought I could pass instance variable between functions (intraclass, at least). In the following code snippet I am unable to do so. Wall It is the first time I've run into this problem.

This is generally fine...
class Foo():
	def __init__(self, *args):
		self.arg0 = arg0
		self.arg1 = arg1
		self.arg2 = arg2
	def bar(self):
		self.thing0 = self.arg0 + self.arg1
		print(self.thing0)
		return self.thing0
	def moo(self):
		self.thing1 = self.thing0/self.arg2
		print('This is : {}.'.format(self.thing1)
		return self.thing1
When I run my code with the following I get an error telling me the class doesn't have any of the attributes I'm printing in print_generic_info(). It shouldn't matter. I've never had this happen before.
class Foo():
    --SNIP--
	def generic_info(self,vrate=0.05):
		self.vrate = vrate
		self.monthly_income = self.income/self.units/12
		self.vmi = (1-self.vrate) * self.monthly_income
		if (0<=self.age<=35):
			self.upkeep = float(0.05 * self.monthly_income)
			self.vupkeep = float(0.05 * self.vmi)
		elif (35<self.age<=60):
			self.upkeep = float(0.1 * self.monthly_income)
			self.vupkeep = float(0.1 * self.vmi)
		else:
			self.upkeep = float(0.2 * self.monthly_income)
			self.vupkeep = float(0.2 * self.vmi)
		self.pretax_income = self.monthly_income - self.upkeep
		self.vpretax_income = self.vmi - self.vupkeep
		return self.monthly_income, self.upkeep, self.pretax_income, self.vmi, self.vupkeep, self.vpretax_income #I am returning properly but they are not  passing through
	def print_generic_info(self):
		print('Your monthly income is...${}. Monthly upkeep is ${}.\nMonthly pretax is ${}.'.format(self.monthly,self.upkeep,self.pretax))
in my main file:
from fooclassfile import Foo
--SNIP--
print_generic_info()
Idk why this is happening. I'm out of ideas . Wall Please help?
Well to start with, this isn't right:
class Foo():
    def __init__(self, *args):
        self.arg0 = arg0
        self.arg1 = arg1
        self.arg2 = arg2
pretty sure you mean this:
class Foo():
    def __init__(self, *args):
        self.arg0 = args[0]
        self.arg1 = args[1]
        self.arg2 = args[2]
Other than that, we need to see your actual error.
Also your calling code at the bottom wouldn't work either as it is not called by an instance of the class and doesn't exist globally.
Quote:It shouldn't matter.
Those attributes don't exist until you run the classes generic_info function as you never initialized them. Please include all your code and error.
Quote:Those attributes don't exist until you run the classes generic_info function as you never initialized them.
OM(G) Eureka!!!

here's the error in code:
while True:
	generic = input('wish to view generic info? [yes/no]')
	if generic =='':
		continue
	elif generic == 'n' or generic == 'no':
        Foo.generic_info()#inside the loop so when print_generic_info() ran, my IV didn't exit...teh debugger is right....AGAIN lol
		print('ok, no problem')
		break
	else:
		Foo.print_generic_info()
		break
print('moving on')
Here's my solution. I imagine it's not as DRY as can be but hey....I'm a WIP (work in progress).
while True:
	generic = input('wish to view generic info? [yes/no]')
	if generic =='':
		continue
	Foo.generic_info()#moved out so it runs regardless
	if generic == 'n' or generic == 'no':
		print('ok, no problem')
		break
	else:
		Foo.print_generic_info()
		break
print('moving on')
FYI yes I am using classes instead of just calling the functions. I find it easier to instantiate objects instead of going with straight function (actually in this instance I need the obj)....Plus I want to become comfortable relying on import...


Thanks!

OT it stinks that I spent hourS trying to figure out such an easy error....