If I understand your question correctly, you should be able to reference class A within class A by putting the type annotation in quotes. This is called forward reference.

class A:
  # do something
  def some_func(self, a: 'A')
  # ...

See ref below

  1. https://github.com/python/mypy/issues/3661
  2. https://www.youtube.com/watch?v=AJsrxBkV3kc
Answer from Chin Pang Yau on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.8 documentation
4 days ago - The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header (ClassName in the example). Class objects support two kinds of operations: attribute references and instantiation.
Discussions

How to reference the class itself in python - Stack Overflow
It could be a variable belonging to the class, method belonging to the class. I want something like self is referencing the instance of the class, but referencing the class. ... in classmethods the first argument passed to function is class (commonly denoted cls) docs.python.org/2/library/... More on stackoverflow.com
🌐 stackoverflow.com
How to reference class variables inside class method
Do you ever set parameters? One, that type hint is actually for instance variables; despite the location, and two, the type hint doesn't create data. It's just a type hint. More on reddit.com
🌐 r/learnpython
11
2
November 5, 2022
How to reference a class variable in python - Stack Overflow
I cant seem to get the classes to read the alive dictionary it keeps throwing a NameError: global name 'import_1' is not defined I am using this guide: http://www.ibiblio.org/g2swap/byteofpython/... More on stackoverflow.com
🌐 stackoverflow.com
Composition - Reference to another class in Python - Stack Overflow
In my example below in Python, object x 'has-an' object y. I'd like to be able to invoke methods of x from y. I'm able to achieve it using @staticmethod, however I'm discouraged to do that. Is there any way(s) to reference the whole Object x from Object y? class X(object): def __init__(self): ... More on stackoverflow.com
🌐 stackoverflow.com
July 16, 2013
🌐
Stack Overflow
stackoverflow.com › questions › 50200657 › how-do-i-reference-to-a-class-using-a-string-in-python
How do I reference to a class using a string in python? - Stack Overflow
What you want to do is make userClass an instance of class X, basing from your code. ... AttributeErrors occur when you are accessing a non-existent attribute in an object. In Python, it's recommended that you use snake case instead of camel case. I.e. userName would be user_name.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-create-and-reference-a-class-variable-in-Python.php
How to Create and Reference a Class Variable in Python
We then create another variable, Student1_finalscore and set it equal to the raw score * the test curve, which gives us the final score. So this is one way we can reference a class variable in Python.
🌐
Reddit
reddit.com › r/learnpython › how to reference class variables inside class method
r/learnpython on Reddit: How to reference class variables inside class method
November 5, 2022 -
class Test:
    config: Dict[str, str]
    parameters: Dict[str, str]

    def default(cls)
        return cls(
                parameters=get_params(),
                config=get_config(file="ATTRIBUTE FROM PARAMETERS"
        )
# I tried it like this:

class Test:
    config: Dict[str, str]
    parameters: Dict[str, str]

    def default(cls)
        return cls(
                parameters=get_params(),
                config=get_config(file=cls.parameters.config_file)
        )
# AttributeError: type object 'Test' has no attribute 'parameters'
🌐
w3tutorials
w3tutorials.net › blog › self-referencing-class-definition-in-python
Self-Referencing Class Definition in Python: How to Reference the Class Name Within Its Declaration — w3tutorials.net
The critical detail here is: during the execution of the class body, the class object does not exist yet, and the name MyClass is not bound in the scope. If you try to reference MyClass inside the body (e.g., x = MyClass), Python raises a NameError because MyClass hasn’t been defined yet.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › How-do-we-reference-Python-class-attributes
How do we reference Python class attributes?
April 15, 2025 - Access both types using dot notation, but remember that class attribute changes affect all instances while instance attribute changes are isolated to individual objects.
🌐
Bite Code
bitecode.dev › bite code! › holding references on classes and functions
Holding references on classes and functions - Bite code!
December 20, 2023 - However there are many other ways to create a variable in Python. The first one is, surprisingly, the import statement: ... This will set the variable datetime in the current namespace, and make it hold a reference to the module object loaded from "datetime.py" in the stdlib.
🌐
W3Schools
w3schools.com › python › python_classes.asp
Python Classes
Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects. ... Note: Each object is independent and has its own copy of the class properties. class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › classmethod.html
classmethod — Python Reference (The Right Way) 0.1 documentation
>>> #This example shows obsolete syntax >>> #Note that method bar can be called on both class and instance objects >>> class Foo: ... def bar(cls): # ... print 'bar’ # ... bar = classmethod(bar) #<= not pythonic anymore, decorator syntax recommended ... >>> Foo.bar <bound method classobj.bar of <class __main__.Foo at 0x00CDAC00>> >>> Foo.bar() bar >>> Foo().bar <bound method classobj.bar of <class __main__.Foo at 0x00CDAC00>> >>> Foo().bar() bar
🌐
University of Chicago Library
www2.lib.uchicago.edu › keith › courses › python › class › 5
Introduction to Python: Class 5
Now we can explain class and instance attribute reference in detail. When looking up an attribute via a class object C, Python first searches the class's name space (C.__dict__); if it doesn't find the attribute, it then recursively searches the class's base classes, left to right and depth first.
🌐
Stack Overflow
stackoverflow.com › questions › 69201113 › python-best-way-to-set-a-reference-for-a-class-attribute-that-has-not-been-defi
Python: Best way to set a reference for a class attribute that has not been defined yet and what default to use? - Stack Overflow
Alternatively, create a WinnerOf class that stores a Match attribute. The name attribute getter could then return None or the player name. In Match, m1 and m2 could be Player or WinnerOf objects. Both ideas boil down to storing a reference to the Match object whose outcome is not known yet, either directly or indirectly. ... How to set python ...
🌐
Stack Overflow
stackoverflow.com › questions › 56340311 › how-to-reference-to-the-existing-object-of-class-in-python-from-string
How to reference to the existing object of class in Python from string? - Stack Overflow
I've got the problem with reference to the object. Function returns string="nameofexistingclass" and I would like to pass the string as an object and not as a class. User choose in the GUI of the existing instance of Person(self.name, self.ID) --> string with name of choosen class --> set another object of different class demanding choosen class.: Classify(person) Python demands to set the self.name, self.ID, but they exist before even choosing.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module4_OOP › ClassInstances.html
Instances of a Class — Python Like You Mean It
It is important to understand that this new instance is distinct from the one that we already created. # `d2` is a new instance of our Dummy class. # It resides at a distinct memory address (0x2ae8f666f60) >>> d2 = Dummy() >>> d2 <__main__.Dummy at 0x2ae8f666f60> >>> d2 is d1 # `d2` and `d1` are distinct instances of `Dummy` False >>> isinstance(d2, Dummy) True · Python’s rules for referencing objects with variables still apply here: assigning an object to a variable, be it a class object or an instance, does not create a distinct copy of that object.
Top answer
1 of 2
5

That reference isn't describing a special case of the language rules, it's a natural result of everything being an object.

The special case is that MyClass(args...) is wired up to create a new object and call MyClass.__init__ (among other things).

It allows you to change the state of the class after it is defined. This can be as simple as changing "static" data based on some configuration.

class EggsApiClient:
    default_url = "example.com"
    def connect(self):
        # do stuff with default_url

if __name__ = "main":
    EggsApiClient.default_url = parse_args("default_url")
2 of 2
3

This is not something that I think most Python users will ever need to do and almost surely shouldn't but it does have a distinct purpose and it's worth understanding.

Consider the following example:

class Foo:
    def set_a(self, a):
        self.a = a

class Bar:
    def set_b(self, b):
        self.b = b

foo = Foo()

foo.set_a(1)

print(foo.a)

bar = Bar()

# interesting part here!
Foo.set_a(bar, 2)

print(bar.a)

Through this feature, we have effectively called set_a on a Bar instance, despite the fact that Bar doesn't have a set_a method defined. That is, if we try:

bar.set_a()

We get an error: AttributeError: 'Bar' object has no attribute 'set_a'.

You might (quite reasonably) ask, "OK, but why would I do that?" Personally, I think I did something like this many moons ago but there was probably a simpler solution to whatever I was trying to accomplish.

I can imagine this be useful in various frameworks e.g. something like unit testing. But the main reason I think you might end up doing this is when you are using a more functional style. For example, you might have a function like this which has no knowledge of Foo or Bar:

def apply(obj, func, *params):
    func(obj, *params)

Which you can then call like so:

apply(bar, Bar.set_b, 3)

print(bar.b)

A real example of the kind of thing you might actually do:

class Person:
    def __init__(self, name: str, student: bool):
        self.name = name
        self.student = student

    def is_student(self):
        return self.student
    
    def __repr__(self):
        return f"{self.name} - student: {self.student}"
    
people = [Person("bob", False), Person("alice", True), Person("carl", True)]

def display(iter):
    print("---")
    for i in iter:
        print(i)


display(people)

display(filter(Person.is_student, people))

Or maybe something like this:

people = map(Person, ["dave", "edith", "frank"], [True, False, True])

display(people)

Check out the functools module for more interesting functional-style approaches like this.

Top answer
1 of 7
4

In the clas body, you're creating the class; there is no self, so you obviously cannot yet refer to self.anything. But also within that body there is as yet no a: name a gets bound AFTER the class body is done. So, although that's a tad less obvious, in the body of class a you cannot refer to a.anything either, yet.

What you CAN refer to are bare names of class attributes that have already been bound: for example, you could simply use 5, spam] at the end of your list lut. spam will be there as a function, as you say at one point that you want; not as a method, and most definitely NOT as a class method (I don't see classmethod ANYWHERE in your code, why do you think a class method would magically spring into existence unless you explicitly wrap a function in the classmethod builtin, directly or by decorator?) -- I suspect your use of "class method" does not actually refer to the class-method type (though it's certainly confusing;-).

So, if you later need to call that function on some instance x of a, you'll be calling e.g. a.lut-1, with the argument explicitly there.

If you need to do something subtler it may be possible to get a bound or unbound method of some sort at various points during processing (after the class creation is done, or, if you want a bound instance method, only after a specific instance is instantiated). But you don't explain clearly and completely enough what exactly it is that you want to do, for us to offer very detailed help on this later-stage alternatives.

2 of 7
4

While you are in the scope of the class, you can just write

class A:
    def spam(self):
        pass

    lut = [1, 2, 3, spam]

a = A()
print a.lut

gives

[1, 2, 3, <function spam at 0xb7bb764c>]

Don't forget that this is a function in your lookup table, not a number as you probably intended. You probably want to solve another problem.