Feeding differeent data types to a class instance?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Mar 13 20:27:56 EST 2010


On Sat, 13 Mar 2010 16:34:55 -0800, kuru wrote:

> I want this class to accept multiple data types but process them as they
> are same when the classs deals with the instances.

The usual term for this is "polymorphism".

> myvec=Vector(1,2,3)
>
> vect=[1,2,3]
> myvec=Vec(vect)

I assume you mean Vector in the last line.

I find this the easiest way to handle this situation:

class Vector(object, *args):
    if len(args) == 1:
        # Assume the caller passed a list argument.
        args = args[0]
    x, y, z = args  # Unpack the arguments.
    # then process as normal.



-- 
Steven



More information about the Python-list mailing list