Python Forum
How can classes access each other Functions and Variables at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can classes access each other Functions and Variables at the same time
#1
How can classes access each other Functions and Variables at the same time? I know you have to use object and self with _init__
I was able to access Class A from Class B but I couldn't access both at the same time!!
For example:

Class A:
Var A1
Var A2
function A1
function A2
function A3
print any member in Class B

Class B:
Var B1
Var B2
function B1
function B2
function B3
print any member in Class A
Reply
#2
you instantiate Class A from class B (in class B) or the other way around.
This is assuming different files for each class, names A.py and B.py
using code like (in class B):
import A

class B:
    def __init__(self):
        class_a = A() # Creates instance of A  named a
        class_a.A1() # Executes method A1 of class A
Reply
#3
class A(object):

  m = 1

  def __init__(self, x):
    self.n = x + 2

class B(object):

  o = 3

  def __init__(self, x):
    self.p = x + 4

c = A(5)
d = B(6)
Output:
>>> A.m 1 >>> c.n 7 >>> B.o 3 >>> d.p 10
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thank you ichabod801,

class A(object):

m = 1

def __init__(self, x):
self.n = x + 2
Using c = A(5) d = B(6) in class A doesn't work, 'B' is not defined
class B(object):

o = 3

def __init__(self, x):
self.p = x + 4

c = A(5)
d = B(6)
Reply
#5
Please use python and output tags when posting code and results. Here are instructions on how to do it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organizing several similar classes with overlapping variables 6hearts 7 1,331 May-07-2023, 02:00 PM
Last Post: 6hearts
  using variables with functions imported from different files. Scordomaniac 3 1,221 May-24-2022, 10:53 AM
Last Post: deanhystad
  Storing whole functions in variables dedesssse 3 2,053 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Getting parent variables in nested functions wallgraffiti 1 2,100 Jan-30-2021, 03:53 PM
Last Post: buran
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,468 Aug-25-2020, 03:43 AM
Last Post: micseydel
  run different functions each time the same button is pressed? User3000 6 3,247 Jul-31-2020, 11:11 PM
Last Post: User3000
  module to store functions/variables and how to call them? mstichler 3 2,345 Jun-03-2020, 06:49 PM
Last Post: mstichler
  local/global variables in functions abccba 6 3,360 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  How to access variables from dirsync module steve_shambles 3 3,794 Apr-02-2020, 08:18 AM
Last Post: steve_shambles
  Python 2.7 passing variables from functions zetto33 1 1,754 Mar-19-2020, 07:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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