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
How can I make a class take an object of the same type as a constructor argument? Currently get unresolved reference...
Firstly, there is no such thing as method overloading in Python[*]. Defining two methods with the same name just means that the second completely replaces the first. This can't work as written. If you really want to do this, one way would be to define a classmethod: class Group(Component): ... @classmethod def from_group(cls, clone: "Group"): return cls(...) although if the idea is just to clone a group you might as well make it an instance method that returns a clone of the current instance. Note, my code above shows the solution to your actual question: you need to put the class name in quotes. An alternative is to import a magic future declaration that enables postponed evaluation of annotations: from __future__ import annotations Then your original declaration will work (although not the code itself, as I note). This was actually supposed to be the default from Python 3.10, but the change has been delayed. [*] there is functools.singledispatchmethod , but it's not used much and it would be pretty strange to use it on the __init__.) More on reddit.com
🌐 r/learnpython
3
1
March 16, 2023
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › how can i make a class take an object of the same type as a constructor argument? currently get unresolved reference...
r/learnpython on Reddit: How can I make a class take an object of the same type as a constructor argument? Currently get unresolved reference...
March 16, 2023 -

I am more familiar with C++ so maybe this isn't the python way to do things, but I want to overload the __init__ function of my class "Component" so that I can construct a new object of that type by passing an object of that type that already exists. Reason for doing it this way is that the class has a parent/child structure and so copying the parent requires careful handling of the family tree, I aim to solve this eventually with recursion, using this overloaded constructor as an aid to avoid bloating my copying function with loads of setter lines setting new objects to their appropriate values.

Currently my class that drives from Component (Group) looks like this:

from Components.component import Component, ComponentType


class Group(Component):
    def __init__(self,
                 component_name: str,
                 ):

        if component_name is None:
            component_name = "New Group"

        super().__init__(component_name=component_name, component_type=ComponentType.Group)

        self.children: list[Component] = []

    def __init__(self, clone: Group): <- This line claims unresolved reference to group, but it is a member of that class!
        pass

    def add_child(self, new_child: Component):
        if new_child not in self.children:
            self.children.append(new_child)
        else:
            print(f"Oh no! {new_child.component_name} is already owned by {self.component_name}!")

    def remove_child(self, old_child: Component):
        if old_child in self.children:
            self.children.remove(old_child)

Feel free to tell me I'm handling this totally wrong! It's just the way that came to mind when looking to solve my current problem haha.

Top answer
1 of 2
2
Firstly, there is no such thing as method overloading in Python[*]. Defining two methods with the same name just means that the second completely replaces the first. This can't work as written. If you really want to do this, one way would be to define a classmethod: class Group(Component): ... @classmethod def from_group(cls, clone: "Group"): return cls(...) although if the idea is just to clone a group you might as well make it an instance method that returns a clone of the current instance. Note, my code above shows the solution to your actual question: you need to put the class name in quotes. An alternative is to import a magic future declaration that enables postponed evaluation of annotations: from __future__ import annotations Then your original declaration will work (although not the code itself, as I note). This was actually supposed to be the default from Python 3.10, but the change has been delayed. [*] there is functools.singledispatchmethod , but it's not used much and it would be pretty strange to use it on the __init__.)
2 of 2
1
I want to overload the init function of my class "Component" so that I can construct a new object of that type by passing an object of that type that already exists. Python doesn't have method overloading (although there is a tool for type-based dispatch in the std lib somewhere) so it would be better to use a factory classmethod for this. But let's talk about why your type declaration can't work: def __init__(self, clone: Group): <- This line claims unresolved reference to group, but it is a member of that class! Ah, but remember, this is Python; there's no compile step. Your class is created in runtime! That means that at the time you're referencing Group, it doesn't exist yet. You're in the process of defining it, still, so there's no value that has that name yet (that's the last thing that happens as you define a class.) You need Self, from typing, to address the circularity, here: from typing import Self # ... def __init__(self, clone: Self): # etc, except you can't have two __init__ methods
🌐
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.
🌐
Altcademy
altcademy.com › blog › how-to-call-a-class-in-python
How to call a class in Python - Altcademy.com
September 10, 2023 - For instance, 'Car' is a concept, and 'my_car' is a specific instance of that concept. A 'Car' has some properties like color, model, brand etc. Similarly, a Python class can have properties and functions. ```python class Car: color = "red" brand = "Toyota" my_car = Car() print(my_car.color) # prints: red print(my_car.brand) # prints: Toyota