1

I want to create some static functions e.g. isEqual.
However, I can't create a function, that is in scope of class X, and also refer to X. <--[1]

MCVE : https://ideone.com/mCjl3l

class X :
    str_myField = "" 
     # parameterized constructor 
    def __init__(self, str_myField ): 
        self.str_myField =str_myField 
    def isEqual(x1 : X,x2: X ):     #<==== name 'X' is not defined
        return x1.myField == x2.myField 
        
X_a = X();
X_a.str_myField ="a"

X_b = X();
X_b.str_myField ="b"
print(">>"+X.isEqual(X_a,X_b))

I got name 'X' is not defined error.

Question

  1. Is there such the limitation [1] in python?
    (It is true in C++, false in Java, but I am new to Python.)

  2. I want to create many static utility functions for X.
    Is it a bad practice to create a new class X_Util (instead of X) to contain such functions instead?

I don't want to remove the explicit type (i.e. I love x1:X more than just x).

Edit

After I got the great link ( How do I type hint a method with the type of the enclosing class? ) suggested by Goion, I have tried to add from __future__ import annotations at the first line.

The solution is :-

from __future__ import annotations

class X :
    str_myField = "" 
     # parameterized constructor 
    def __init__(self ): 
        self.str_myField ="default"
    def isEqual(x1 : X,x2: X ):
        return x1.str_myField == x2.str_myField 
        
X_a = X();
X_a.str_myField ="a"

X_b = X();
X_b.str_myField ="b"
print(">>"+str(X.isEqual(X_a,X_b)))
cppBeginner
  • 1,114
  • 9
  • 27
  • 1
    instead of type hinting you can perform a check with `isinstance(x1, X)` – Cresht Jun 12 '21 at 04:57
  • @Bill :: Thank a lot. Does such change have some negative impact on auto-complete? – cppBeginner Jun 12 '21 at 04:58
  • 1
    i'm sorry, i'm not sure i can answer that question. but i would add that to perform such functions like `.equals()` you might want to try dunder methods (https://www.geeksforgeeks.org/dunder-magic-methods-python/) like `__eq__()`, which would override the `==` operator. – Cresht Jun 12 '21 at 05:00
  • 1
    Maybe this can help [How do I type hint a method with the type of the enclosing class?](https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class) – Yoshikage Kira Jun 12 '21 at 05:01
  • 1
    Python uses `==` to check equality. The behavior can be overwriting by implementing `__eq__`. Your method violates a couple of other conventions as well: snake_case names, `self` as the first argument... – Klaus D. Jun 12 '21 at 05:03
  • @Goion :: It is really useful. Thank for the great link. – cppBeginner Jun 12 '21 at 05:03
  • @Klaus D. :: I never know that my code is that bad. Thanks, I will study those aspects. ^^ – cppBeginner Jun 12 '21 at 05:05
  • @Carcigenicate :: at the very first glance, no ( https://ideone.com/QVTvoB ) Perhaps, I need to read it more. – cppBeginner Jun 12 '21 at 05:07
  • 1
    That's a separate error. You said that the constructor takes one argument, but you don't supply one when creating the object. – Carcigenicate Jun 12 '21 at 05:08
  • @Carcigenicate :: oh no, i am sorry. It works now. – cppBeginner Jun 12 '21 at 05:11
  • @Carcigenicate :: I just did it. Thank! – cppBeginner Jun 12 '21 at 05:13

0 Answers0