What to return in Factory Methods?

I have a class Node which I want it to have multiple constructors.

I was reading online about factory methods and apparently, it is the cleanest Pythonic way of implementing constructors. My class looks as follows so far:

class Node(object):
  element = None
  left = None
  right = None

  def __init__(self, element):
    self.element = element

  def tree(cos, element, left, right):
    self.element = element
    self.left = left
    self.right = right
    # return here

What am I supposed to return here though? All examples I saw online had only one assignment and they would return that very assignment. I have three assignments. What is appropriate to return here?


In a named constructor (factory method), you should create an object that you want to return. E.g.

class Node(object):
    def __init__(self, element):
        self.element = element
        self.left = self.right = None

    def tree(cls, element, left, right):
        node = cls(element)
        node.left = left
        node.right = right
        return node

Note that you don't need the class members, and having class members with the same name as instance members is a bad idea.

Need Your Help

MySQL help figuring out structure of databases

mysql database conceptual

I am a java programmer trying to make the jump to web development and database management. I am trying to figure out the structure of web services in general and I will try to ask some questions that

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.