Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling a variable by name
#7
(Oct-16-2016, 07:46 PM)sunhear Wrote: i am trying to implement the changes in the code. I am new at programming objects and classes.

The original question was how to right a function/method to add 2 variables together when only 1 is passed as an argument.

By using "self" to reference the other instance variables in your object.  Here, we create an object, then instantiate it, and call a method of that instance, passing only one argument.  So we do math with two variables, even though we only passed one.
>>> class Spam(object):
...   def __init__(self):
...     self.eggs = 42
...   def add(self, other):
...     return self.eggs + other
...
>>> fork = Spam()
>>> fork.add(9)
51
Reply


Messages In This Thread
calling a variable by name - by sunhear - Oct-14-2016, 08:49 PM
RE: calling a variable by name - by Yoriz - Oct-14-2016, 09:56 PM
RE: calling a variable by name - by micseydel - Oct-14-2016, 09:58 PM
RE: calling a variable by name - by ichabod801 - Oct-14-2016, 10:34 PM
RE: calling a variable by name - by sunhear - Oct-16-2016, 07:46 PM
RE: calling a variable by name - by nilamo - Oct-17-2016, 04:56 PM
RE: calling a variable by name - by metulburr - Oct-17-2016, 12:15 AM
RE: calling a variable by name - by ichabod801 - Oct-17-2016, 08:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 902 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Calling a base class variable from an inherited class CompleteNewb 3 1,767 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
Question Calling on a Variable using other Variables jacknewport 4 2,069 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,302 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  calling a variable in a for loop in windows dwaynes 1 1,844 Apr-02-2020, 05:21 PM
Last Post: mcmxl22
  calling an object variable in a dictionary sunhear 3 4,362 Dec-30-2016, 05:28 PM
Last Post: sunhear

Forum Jump:

User Panel Messages

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