receive an object in a method of the same type as the method class

Asked

Viewed 38 times

0

I’m passing a java code to python and came across it(cutting a few parts)

public class TreeNode {
    public TreeNode(TreeNode pai) {

how do I receive the father of the type Treenode in python?

class TreeNode:
    def __init__(self, pai: TreeNode):

that of the "Unresolved Re-reference 'Treenode' what way to do this?

  • What exactly would be "father of the type"? Do you need to reference the class itself? Remember that Python, unlike Java, has dynamic typing, so it makes no sense for you to define a type in the argument - and even if you do it will not interfere with the execution of the code; it will only be a annotation type. See more on Python 3: Attribute Types

1 answer

1


In python you need to do the test inside the method/constructor, ex:

class TreeNode:
  def __init__(self, pai):
    if not isinstance(pai, TreeNode):
      print('Nao Ok')
    else:
      print('Ok')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.