Python 3: Defining a class type inside a class?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

sgfw
Posts: 46
Joined: Wed Jun 19, 2013 11:47 pm UTC

Python 3: Defining a class type inside a class?

Postby sgfw » Sat Jan 11, 2014 10:43 pm UTC

So, I have a code that resembles a more complicated version of this:

Code: Select all

class C:
    t = type(C(1))
    def __init__(self, a):
        self.whatever = a
    def somefunction(self, b):
        if type(b) == C.t:
            self.whatever = f1(b)
        else:
            self.whatever = f2(b)


My problem is with the line "t = type(C(1))" I need a function to be able to recognize if an argument is of the type created by the class, but I do not want to need to initialize the class inside the class in order to do that. Is there a way around this?

Nyktos
Posts: 138
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: Python 3: Defining a class type inside a class?

Postby Nyktos » Sat Jan 11, 2014 11:47 pm UTC

Is there a reason you can't just do "isinstance(b, C)"?

sgfw
Posts: 46
Joined: Wed Jun 19, 2013 11:47 pm UTC

Re: Python 3: Defining a class type inside a class?

Postby sgfw » Sun Jan 12, 2014 12:11 am UTC

Is there a reason you can't just do "isinstance(b, C)"?


Yes- I didn't know that existed. Thanks for the help, that solved my problem.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests