A question about funcation parameter and self defined object

2008-10-08 Thread Wei Guo
Hi,

I defined a class called vec3 which contains x, y, z and in another
function, I tried to call a function which takes a vec3 as a parameter, but
it seems that parameter is passed as a generic object and I can not access x
, y, z in my vec3. Could anyone help me with that?

 class vec3:
def __init__(self, x_ = 0.0, y_ = 0.0, z_ = 0.0):
self.x = x_
self.y = y_
self.z = z_
class mat4:
 def translation( traV = vec3() ):
  tranM.rowLst[index][0] = traV.x
AttributeError: 'NoneType' object has no attribute 'x'

Could anyone help me how to turn the traV as type of vec3() instead of
NoneType object?

Thanks a lot,

Wei
--
http://mail.python.org/mailman/listinfo/python-list


Re: A question about funcation parameter and self defined object

2008-10-08 Thread Wei Guo
Hi Chris,

Thanks a lot for reply, you are right. I want to use this method as a static
method as:

translation = staticmethod( translation )

I think that here the built in function pass None. So we can not pass any
self defined object for static method?

Best regards,

Wei

On Wed, Oct 8, 2008 at 5:50 PM, Chris Rebert <[EMAIL PROTECTED]> wrote:

> On Wed, Oct 8, 2008 at 4:36 PM, Wei Guo <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I defined a class called vec3 which contains x, y, z and in another
> > function, I tried to call a function which takes a vec3 as a parameter,
> but
> > it seems that parameter is passed as a generic object and I can not
> access x
> > , y, z in my vec3. Could anyone help me with that?
>
> Being dynamically typed, Python has no notion of variables having
> types, so the object isn't being "passed as a generic object", you're
> getting what really is a "generic object" value of type NoneType,
> which means the value of traV is indeed None, not vec3().
>
> >
> > class vec3:
> > def __init__(self, x_ = 0.0, y_ = 0.0, z_ = 0.0):
> > self.x = x_
> > self.y = y_
> > self.z = z_
> > class mat4:
> >  def translation( traV = vec3() ):
> >   tranM.rowLst[index][0] = traV.x
> > AttributeError: 'NoneType' object has no attribute 'x'
>
> This code is perfectly fine. See below.
>
> >
> > Could anyone help me how to turn the traV as type of vec3() instead of
> > NoneType object?
>
> That's not what's happening. It's not like traV is being cast to
> NoneType thus making x inaccessible, as that's not even possible to
> express in Python.
> What's happening is something is calling translation() with None as an
> argument, and of course None (the value the caller provided for traV)
> has no attribute 'x', hence the error. So, check the full exception
> traceback and see who's calling translation() and how the argument
> being passed to it got to be None.
>
> Cheers,
> Chris
> --
> Follow the path of the Iguana...
> http://rebertia.com
>
> >
> > Thanks a lot,
> >
> > Wei
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: A question about funcation parameter and self defined object

2008-10-09 Thread Wei Guo
Hi Terry,

Thanks for your reply. But the reason I want to have that is for not
changing the functions which already based on translation functions.

If there is any idea how to bring parameter in static method, that will be
great.

Wei
On Wed, Oct 8, 2008 at 8:24 PM, Terry Reedy <[EMAIL PROTECTED]> wrote:

> Wei Guo wrote:
>
>> Hi Chris,
>>  Thanks a lot for reply, you are right. I want to use this method as a
>> static method as:
>>  translation = staticmethod( translation )
>>
>
> static methods are mostly useless in Python.  Just put the definition of
> translation outside of any class.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


A question about string and float number

2008-08-06 Thread Wei Guo
Hi all,

I am new of python. Could anyone help me a question as below?

Is there any function that can judge a string s is a float number or not?
FOr example, if s = '1.232' or s='1e+10', then it returns true, otherwise,
it will return false.

isdigit() in string doesn't work. float() will throw an exception and I just
need true or false as result.

Thanks a lot in advance,

Wei
--
http://mail.python.org/mailman/listinfo/python-list

Re: A question about string and float number

2008-08-07 Thread Wei Guo
Hi Thanks for Tyler and Edwin's help.

For my questions, I need to import some xml file and there are floating
number and strings in it. I need to process string and number differently.
This is reason that I am asking question here. Is this background
information we need for this quesions.

Btw, which way is better? type or with exception ValueError?

Thanks,

Wei


On 8/6/08, Tyler Breisacher <[EMAIL PROTECTED]> wrote:
>
> It's generally a bad idea to use "except" without naming a specific
> exception. The exception you might expect in this case is ValueError. Any
> other exception *should* be uncaught if it happens. By the way, this method
> will return true for integers as well as floats. For example, isFloat('3')
> will return 3.0. So make sure this is what you want, since it wasn't 100%
> clear from the original message.
>
>
> Wei Guo wrote:
>
>> #this is a better way of testing a string for float
>> def isFloat(s):
>>try:
>>s = float(s)
>>except:
>>return False
>>return True
>>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: A question about string and float number

2008-08-07 Thread Wei Guo
Hi I tried the first type method but it seens that it doesn't work. Could
anyone help me about it?

>>> s = '3.145'
>>> type(s) == type(float())
False
>>> type(s)

>>> type(float())

>>>

Best regards,

Wei


On 8/7/08, Wei Guo <[EMAIL PROTECTED]> wrote:
>
> Hi Thanks for Tyler and Edwin's help.
>
> For my questions, I need to import some xml file and there are floating
> number and strings in it. I need to process string and number differently.
> This is reason that I am asking question here. Is this background
> information we need for this quesions.
>
> Btw, which way is better? type or with exception ValueError?
>
> Thanks,
>
> Wei
>
>
>  On 8/6/08, Tyler Breisacher <[EMAIL PROTECTED]> wrote:
>>
>> It's generally a bad idea to use "except" without naming a specific
>> exception. The exception you might expect in this case is ValueError. Any
>> other exception *should* be uncaught if it happens. By the way, this method
>> will return true for integers as well as floats. For example, isFloat('3')
>> will return 3.0. So make sure this is what you want, since it wasn't 100%
>> clear from the original message.
>>
>>
>> Wei Guo wrote:
>>
>>> #this is a better way of testing a string for float
>>> def isFloat(s):
>>>try:
>>>s = float(s)
>>>except:
>>>return False
>>>return True
>>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: A question about string and float number

2008-08-07 Thread Wei Guo
Hi Grant,

I am using the try/exception and just curious why function type doesn't
work.

Best regards,

Wei


On 8/7/08, Timothy Grant <[EMAIL PROTECTED]> wrote:
>
> That's because s IS a string. It's not been converted to a float.
>
> In [1]: s = '3.1415'
> In [2]: n = float(s)
> In [3]: type(s)
> Out[3]: 
> In [4]: type(n)
> Out[4]: 
>
> Why are you avoiding the very simple try:/except: solution to this problem?
>
> On Thu, Aug 7, 2008 at 1:28 PM, Wei Guo <[EMAIL PROTECTED]> wrote:
> > Hi I tried the first type method but it seens that it doesn't work. Could
> > anyone help me about it?
> >
> >>>> s = '3.145'
> >>>> type(s) == type(float())
> > False
> >>>> type(s)
> > 
> >>>> type(float())
> > 
> >>>>
> >
> > Best regards,
> >
> > Wei
> >
> >
> > On 8/7/08, Wei Guo <[EMAIL PROTECTED]> wrote:
> >>
> >> Hi Thanks for Tyler and Edwin's help.
> >>
> >> For my questions, I need to import some xml file and there are floating
> >> number and strings in it. I need to process string and number
> differently.
> >> This is reason that I am asking question here. Is this background
> >> information we need for this quesions.
> >>
> >> Btw, which way is better? type or with exception ValueError?
> >>
> >> Thanks,
> >>
> >> Wei
> >>
> >>
> >> On 8/6/08, Tyler Breisacher <[EMAIL PROTECTED]> wrote:
> >>>
> >>> It's generally a bad idea to use "except" without naming a specific
> >>> exception. The exception you might expect in this case is ValueError.
> Any
> >>> other exception *should* be uncaught if it happens. By the way, this
> method
> >>> will return true for integers as well as floats. For example,
> isFloat('3')
> >>> will return 3.0. So make sure this is what you want, since it wasn't
> 100%
> >>> clear from the original message.
> >>>
> >>>
> >>> Wei Guo wrote:
> >>>>
> >>>> #this is a better way of testing a string for float
> >>>> def isFloat(s):
> >>>>try:
> >>>>s = float(s)
> >>>>except:
> >>>>return False
> >>>>return True
> >>>
> >>>
> >>> --
> >>> http://mail.python.org/mailman/listinfo/python-list
> >>
> >
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
>
> --
> Stand Fast,
> tjg.  [Timothy Grant]
>
--
http://mail.python.org/mailman/listinfo/python-list