Python Forum
can't pass instance variables between functions?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't pass instance variables between functions?
#1
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?
Reply
#2
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.
Reply
#3
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....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 845 Jul-27-2023, 12:40 AM
Last Post: tester_V
  using variables with functions imported from different files. Scordomaniac 3 1,261 May-24-2022, 10:53 AM
Last Post: deanhystad
  How to pass variables from one class to another hobbyist 18 10,609 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Storing whole functions in variables dedesssse 3 2,083 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Getting parent variables in nested functions wallgraffiti 1 2,120 Jan-30-2021, 03:53 PM
Last Post: buran
  Why Pass Functions as arguments? muzikman 14 5,587 Jan-18-2021, 12:08 PM
Last Post: Serafim
  Do I have to pass 85 variables to function? Milfredo 10 4,233 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,542 Sep-07-2020, 08:02 AM
Last Post: perfringo
  module to store functions/variables and how to call them? mstichler 3 2,372 Jun-03-2020, 06:49 PM
Last Post: mstichler
  local/global variables in functions abccba 6 3,412 Apr-08-2020, 06:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020