0

What is wrong with defining a class constructor like this:

I am trying to construct two different objects based on whether input d stays None or gets value assigned.

class MSMeshFace(object):

    def __init__(self, a= None, b= None, c= None, d= None):
            self.a = a
            self.b = b
            self.c = c
            self.d = d
            if self.d == None:
                    triangleFace = MSMeshFace(self.a, self.b, self.c)
                    self.count = 3
                    return triangleFace
            else:
                    quadFace = MSMeshFace(self.a, self.b, self.c, self.d)
                    self.count = 4
                    return quadFace
6
  • 1
    You can't return values from an init function. Well, you can, but the result gets thrown away. Commented Dec 5, 2014 at 15:41
  • 3
    Also, this probably results in an infinite-loop of constructors. Why are you doing this? What's your goal? Commented Dec 5, 2014 at 15:42
  • This definition is recursive. Commented Dec 5, 2014 at 15:44
  • 2
    Do you actually want "two different objects", or are you just looking for a way to set count based on whether or not d is supplied? Commented Dec 5, 2014 at 15:49
  • I wanted a way to construct that object from either inputs abc, or abcd. so when I call MSMeshFace(a,b,c) i get a triangleFace and when i call MSMeshFace(a,b,c,d) i get a quadFace Commented Dec 5, 2014 at 16:13

3 Answers 3

2

If you are trying to return an object of different types based on arguments, make a new function like:

def make_face(*args):
    if len(args) == 3:  # triangle face
        return TriMeshFace(*args)
    else:  # quad face
        return QuadMeshFace(*args)

You can't (normally) change type in a constructor (you may be able to in __new__, though, but you don't need that for this). If you want to add functions to MSMeshface (as you suggest in the comments), define a base class containing those functions such as:

class MeshBase:
    def addData(self, data): pass
    def ToDSMeshFace(self): pass

class TriMeshFace(MeshBase): pass

class QuadMeshFace(MeshBase): pass
Sign up to request clarification or add additional context in comments.

3 Comments

how can i do that inside of a class instead of a function? like i want ability to call MSMeshFace(a,b,c) and get TriMeshFace back but also if i call MSMeshface(a,b,c,d) and get QuadMeshFace
Just call your function MSMeshface? Why does it have to be inside the class?
i would like to have class functionality so that i can add more functions to it ex. MSMeshFace(a,b,c).addData() or MSMeshFace(a,b,c).toDSMeshFace()
2

The constructor (well, the initializer, really) is not supposed to return anything, it's supposed to initialize a newly created instance. What you want is:

class MSMeshFace(object):
    def __init__(self, a=None, b=None, c=None, d=None):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.count = 3 if self.d is None else 4

Comments

0

Your intention seems to be as follows:

class MSMeshFace(object):
    def __init__(self, a= None, b= None, c= None, d= None):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.count = 3 if self.d is None else 4

The constructed object will be returned automatically. The object already exists, and is pointed by self. You can't influence the moment of creation of the object in the class's __init__ method, you can only initialize it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.