Python Forum
can Inner Class reference the Outer Class's static variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can Inner Class reference the Outer Class's static variable?
#4
Class variable, not static variable. This is Python, not C++.

The answer to your question is yes and no. You cannot use class variables in Outer when assigning class variables in Inner because class Outer does not exist yet when you create class variables for Inner. However, methods in Inner can see Outer just fine.
class Outer(object):
    a = 1
 
    class Inner(object):
        a = 2

        @classmethod
        def aay(cls):
            return cls.a, Outer.a

print(Outer.Inner.aay())
Output:
(2, 1)
This also works for instance methods of Inner.
Reply


Messages In This Thread
RE: can Inner Class reference the Outer Class's static variable? - by deanhystad - Mar-21-2022, 01:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 9 423 Yesterday, 08:46 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 252 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 259 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 628 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Pandas Outer merge skollu826 2 287 Apr-20-2024, 06:28 PM
Last Post: deanhystad
  class definition and problem with a method HerrAyas 2 331 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 382 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 479 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 642 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 818 Feb-19-2024, 03:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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