-3

I'm making a login program for fun, and I was wondering how do you access functions in one class/def in another class/def? Sorry if I sound dumb I'm quite the noob in Python!

Example:

class Main()
     def LOL:
        A = 'apples'

and I want function A to be applied here:

def banana:
    B = 'banana'
    print(B + A)

Sorry if its quite a random code example but I couldn't really think of anything else!

Wooble
  • 87,717
  • 12
  • 108
  • 131
  • _"I want function A to be applied here"_. Your example doesn't appear to have a function named "A". – Kevin Jul 09 '14 at 16:51

1 Answers1

0
class Main():
   def __init__(self):
      self.A = 'apples' #A is now an instance variable, set to 'apples'

#don't forget parentheses here!
def banana():
   B = 'banana'
   m = Main() #you'll need an instance of main to reference a variable in it...
   print(B + m.A) #m.A gets the variable A from m.

Something more similar to your example, but requires more code (using a function to get a variable):

class Main():
   def __init__(self):
      self.A = 'apples' #A is now an instance variable, set to 'apples'
   def LOL(self):
      return self.A
#don't forget parentheses here!
def banana():
   B = 'banana'
   m = Main() #you'll need an instance of main to reference a variable in it...
   print(B + m.LOL()) #m.LOL() will return m.A
skyress3000
  • 59
  • 1
  • 7