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?
#1
In the nested class, can Inner Class reference the Outer Class's static variable?

class Outer(object):
    a = 111

    class Inner(object):

        b = Outer.a.   # NameError: name 'Outer' is not defined

        @staticmethod
        def get_a(cls):
            return Outer.a  # OK
Reply
#2
why make your code complicated?

why not:
class Outer(object):
    def __init__(self):
        self.a = 111

print(f"Outer.a = {Outer().a}")
Output:
Outer.a = 111
Reply
#3
You can do this
class Outer(object):
    a = 111
 
    class Inner(object):

        @staticmethod
        def get_a(cls):
            return Outer.a  # OK

    Inner.b = a
Reply
#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
#5
Find a way to handle it now, using inheritance

class _(object):
    a = 100

class Outer(_):

    class Inner(object):
        b = _.a   # OK
 
        @staticmethod
        def get_a(cls):
            return Outer.a  # OK
Reply
#6
What are you trying to achieve? I was confused by your original post and more confused now. Why do you want a class variable in Inner that is initialized to the value of a class variable in Outer? I don't see how this makes any sense. Could you please describe the purpose for this?
Reply
#7
Unlike inner class, a static nested class cannot access the member variables of the outer class. It is because the static nested class doesn't require you to create an instance of the outer class. OuterClass. NestedClass obj new OuterClass.We can not have a « url snipped » static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself. For example, the following program doesn't compile.
Gribouillis write Jul-01-2022, 07:56 AM:
Offsite link promotion snipped. Pleas read What to NOT include in a post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 331 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Pandas Outer merge skollu826 2 140 Apr-20-2024, 06:28 PM
Last Post: deanhystad
  class definition and problem with a method HerrAyas 2 251 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 288 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 366 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 532 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 654 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 735 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  problem usage of static method akbarza 5 539 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Class test : good way to split methods into several files paul18fr 4 485 Jan-30-2024, 11:46 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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