Velocity Reviews > type of simple object

# type of simple object

Pierre Barbier de Reuille
Guest
Posts: n/a

 02-02-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> Thank you guys.
>
> My function should multiply every element of a list, for example
> "something"
> and "something" can be an integer or another list.
> If it deals with integer than it is ok, but
> If it deals with list than it become false for example list*2 =
> listlist, and what I really want is to mutlitply its member.
> That's why I need to know the type of my data in "something".

As stated by another comment, I would do something like :

def multiply(object, factor):
try:
return [ multiply(i,factor) for i in object ]
except TypeError:
return object*factor

This function will, recursively multiply a nested list of numbers by
"factor" ...

>
> By the way I am new in python, I heard that it has a MatLab
> capabilities, How good is that? Since It would be very nice when we can
> do what MatLab do in python.....

I think you are referring to the Numeric or the numarray modules. They
offer matric computations close to chat Matlab offers. "numarray" is the
newer version of "Numeric", but in case of small matrix, it performs
slower (for various reasons). Then, you can find lots of information on
the net concerning these two modules.

>
>
> Sincerely Yours,
> pujo
>

Pierre

Steve Holden
Guest
Posts: n/a

 02-02-2005
Pierre Barbier de Reuille wrote:

> (E-Mail Removed) a écrit :
>
>> Thank you guys.
>>
>> My function should multiply every element of a list, for example
>> "something"
>> and "something" can be an integer or another list.
>> If it deals with integer than it is ok, but
>> If it deals with list than it become false for example list*2 =
>> listlist, and what I really want is to mutlitply its member.
>> That's why I need to know the type of my data in "something".

>
>
> As stated by another comment, I would do something like :
>
> def multiply(object, factor):
> try:
> return [ multiply(i,factor) for i in object ]
> except TypeError:
> return object*factor
>
> This function will, recursively multiply a nested list of numbers by
> "factor" ...
>

As a matter of good practice it's usually considered unwise to shadow
names of system types like "dict" and "object", though there wouldn't be
any problems in this case except the infinite recursion. Which
definitely *would* be a problem.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/

Pierre Barbier de Reuille
Guest
Posts: n/a

 02-02-2005
Steve Holden a écrit :
> Pierre Barbier de Reuille wrote:
>
>> (E-Mail Removed) a écrit :
>>
>>> Thank you guys.
>>>
>>> My function should multiply every element of a list, for example
>>> "something"
>>> and "something" can be an integer or another list.
>>> If it deals with integer than it is ok, but
>>> If it deals with list than it become false for example list*2 =
>>> listlist, and what I really want is to mutlitply its member.
>>> That's why I need to know the type of my data in "something".

>>
>>
>>
>> As stated by another comment, I would do something like :
>>
>> def multiply(object, factor):
>> try:
>> return [ multiply(i,factor) for i in object ]
>> except TypeError:
>> return object*factor
>>
>> This function will, recursively multiply a nested list of numbers by
>> "factor" ...
>>

> As a matter of good practice it's usually considered unwise to shadow
> names of system types like "dict" and "object", though there wouldn't be
> any problems in this case except the infinite recursion. Which
> definitely *would* be a problem.

Oops ... indeed, I usually try not to do so ^_^
That's why I usually use "obj" more than "object" and that most of the
time I use a name more _on the topic_ ...

Thx for the correction

Pierre

>
> regards
> Steve

Colin J. Williams
Guest
Posts: n/a

 02-02-2005
(E-Mail Removed) wrote:
> Thank you guys.
>
> My function should multiply every element of a list, for example
> "something"
> and "something" can be an integer or another list.
> If it deals with integer than it is ok, but
> If it deals with list than it become false for example list*2 =
> listlist, and what I really want is to mutlitply its member.
> That's why I need to know the type of my data in "something".
>
> By the way I am new in python, I heard that it has a MatLab
> capabilities, How good is that? Since It would be very nice when we can
> do what MatLab do in python.....
>
>
> Sincerely Yours,
> pujo
>

If you are looking for MatLab like facilities you might consider
numarray, available from sourceforge.

Colin W.