Re: type conversion
On Jan 3, 10:23 am, r rt8...@gmail.com wrote: On Jan 2, 7:46 pm, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: ...incessant rambling about a news reader , 101 excuses for butting into a thread [snip] ... public display of ignorance of newsgroup ethics, 101 excuses for not knowing proper terminologies. Throw your newsreader in the garbage and use Google groups, less headache, more filling! Really? I found Google Groups lacking for many thing. Google Groups' only advantage is being browser-based. No need to worry about hidden headers And you may even get a star or 2 :) Don't you realize that even Google Groups handles those hidden headers too. And that the hidden headers aren't really hidden, even in Google Groups. If people are ignoring a thread, they won't even see your post even though you have changed the subject line. Yea NO $#Y, that makes a lot of sense, if i am ignoring something i course i will not see it! OK, Steven so you did not go off topic you simply high-jacked this thread. I get it now :) Yea NO $#Y, that doesn't make sense. Yea or NO? or $#Y? -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
r wrote: On Jan 1, 4:40 pm, Robert Kern robert.k...@gmail.com wrote: Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? I recommend keeping the __init__() as dumb as possible. Ideally, it should just assign to attributes. I would add a fromfoo() classmethod for each foo that I wanted to support. If I really wanted an all-singing, all-dancing initialization method, I would add another classmethod that would just dispatch to the appropriate type-specific classmethod. I prefer classmethods to plain functions because I can subclass. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco Why are you busting into this thread? Are you talking to me, or Hamish, who (presumably unintentionally) responded to a message instead of making a new thread? If you want to suggest that one should have more care about starting new threads, that's fine, but please do not jump to the conclusion that there is malintent. And don't chastise people for not being as rude as you. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
On Jan 2, 1:46 pm, Robert Kern robert.k...@gmail.com wrote: r wrote: On Jan 1, 4:40 pm, Robert Kern robert.k...@gmail.com wrote: Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? I recommend keeping the __init__() as dumb as possible. Ideally, it should just assign to attributes. I would add a fromfoo() classmethod for each foo that I wanted to support. If I really wanted an all-singing, all-dancing initialization method, I would add another classmethod that would just dispatch to the appropriate type-specific classmethod. I prefer classmethods to plain functions because I can subclass. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco Why are you busting into this thread? Are you talking to me, or Hamish, who (presumably unintentionally) responded to a message instead of making a new thread? If you want to suggest that one should have more care about starting new threads, that's fine, but please do not jump to the conclusion that there is malintent. And don't chastise people for not being as rude as you. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco I am not so much chastising Hamish as i am Steven, Steven knows better than this BS, and has preached to so many people about off topic post that my ears are bleeding from his incessant rambling about it. Now, he goes and does what he complains about s much. I'm just calling him on it thats all. I can't speak for the other responders because i know of none of them. -- http://mail.python.org/mailman/listinfo/python-list
RE: type conversion
I actually have no idea what ur talking about... aren't conversations threaded by subject? -Original Message- From: python-list-bounces+hamish=valvesoftware@python.org [mailto:python-list-bounces+hamish=valvesoftware@python.org] On Behalf Of r Sent: Friday, January 02, 2009 12:12 PM To: python-list@python.org Subject: Re: type conversion On Jan 2, 1:46 pm, Robert Kern robert.k...@gmail.com wrote: r wrote: On Jan 1, 4:40 pm, Robert Kern robert.k...@gmail.com wrote: Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? I recommend keeping the __init__() as dumb as possible. Ideally, it should just assign to attributes. I would add a fromfoo() classmethod for each foo that I wanted to support. If I really wanted an all-singing, all-dancing initialization method, I would add another classmethod that would just dispatch to the appropriate type-specific classmethod. I prefer classmethods to plain functions because I can subclass. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco Why are you busting into this thread? Are you talking to me, or Hamish, who (presumably unintentionally) responded to a message instead of making a new thread? If you want to suggest that one should have more care about starting new threads, that's fine, but please do not jump to the conclusion that there is malintent. And don't chastise people for not being as rude as you. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco I am not so much chastising Hamish as i am Steven, Steven knows better than this BS, and has preached to so many people about off topic post that my ears are bleeding from his incessant rambling about it. Now, he goes and does what he complains about s much. I'm just calling him on it thats all. I can't speak for the other responders because i know of none of them. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
Hamish McKenzie schrieb: I actually have no idea what ur talking about... aren't conversations threaded by subject? Nope, they are threaded by message id. The subject is used as fallback only. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
On Fri, 02 Jan 2009 15:49:25 -0800, Hamish McKenzie wrote: I actually have no idea what ur talking about... aren't conversations threaded by subject? Not usually. When you hit Reply to a post, your post gets a hidden header line that says I'm a reply to post #12345 (whatever post number it is). Just because you change the subject line doesn't change that. Most newsreader programs thread by that header, not the subject. Consequently most people who view posts in thread order will see: Subject X Y Z +-- Re: Subject X Y Z +-- Re: Subject X Y Z +-- Re: Subject X Y Z +-- Re: Subject X Y Z +-- Hello I'm changing the subject line +-- Re: Hello I'm changing the subject line +-- Re: Subject X Y Z +-- Re: Subject X Y Z This has the serious disadvantages that: (1) It annoys people who do threading; and (2) If people are ignoring a thread, they won't even see your post even though you have changed the subject line. It is considered rude to hijack a thread for a completely new subject, although it is acceptable to change the subject line if the thread gradually evolves to a new discussion: Subject X Y Z +-- Re: Subject X Y Z +-- Re: Subject X Y Z +-- Subject A B C [was Re: Subject X Y Z] +-- Re: Subject A B C [was Re: Subject X Y Z] Don't mistake the above for off-topic posting, which is something completely different, nor for cross-posting, which is different again. Off-topic posts are ones that don't have anything to do with the news group they are sent to, e.g. a message about Ruby sent to a Python list. It is barely acceptable to occasionally post brief off-topic messages that are particularly amusing or important if you label them such with [OT] or [off-topic] in the subject line. Cross-posting is sending a single message to more than one newsgroup. If you absolutely must cross-post, you should always set the Followup-To header to *one* newsgroup. If you don't know how to set followups, then don't cross-post. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
On Jan 2, 7:46 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: ...incessant rambling about a news reader , 101 excuses for butting into a thread [snip] Throw your newsreader in the garbage and use Google groups, less headache, more filling! No need to worry about hidden headers And you may even get a star or 2 :) If people are ignoring a thread, they won't even see your post even though you have changed the subject line. Yea NO $#Y, that makes a lot of sense, if i am ignoring something i course i will not see it! OK, Steven so you did not go off topic you simply high-jacked this thread. I get it now :) -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? I recommend keeping the __init__() as dumb as possible. Ideally, it should just assign to attributes. I would add a fromfoo() classmethod for each foo that I wanted to support. If I really wanted an all-singing, all-dancing initialization method, I would add another classmethod that would just dispatch to the appropriate type-specific classmethod. I prefer classmethods to plain functions because I can subclass. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
RE: type conversion
You could also use a dict with type:method key/value pairings. This is closer to a switch/case than an if...elif chain is. of course, that's a great idea... thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
On Jan 1, 4:40 pm, Robert Kern robert.k...@gmail.com wrote: Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? I recommend keeping the __init__() as dumb as possible. Ideally, it should just assign to attributes. I would add a fromfoo() classmethod for each foo that I wanted to support. If I really wanted an all-singing, all-dancing initialization method, I would add another classmethod that would just dispatch to the appropriate type-specific classmethod. I prefer classmethods to plain functions because I can subclass. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco Why are you busting into this thread? why have you not created your own thread. Someone asked a question about recycle bin access and you come in here, bust down the door and ask a completely off topic question, which is OK if you start a new thread of your own. And the most insane part to all of this, is that Steven just plays right along?!?!? Come on Stevie, you know better than this! PS (To OP) mark hammonds win32 package will not do what you ask, sorry -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
On Wed, 31 Dec 2008 12:35:20 -0800, Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. Type conversion is a bit of a misleading subject line. You're not really converting different types, just initialising from different types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? You could have an external function qtom: def qtom(quaternion): a, b, c, d = quaternion return Matrix([ a, 0, 0, 0, 0, b, 0, 0, 0, 0, c, 0, 0, 0, 0, d]) But the first two solutions seem reasonable to me, except of course Python doesn't have a case statement you have to use an if...elseif block. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: type conversion
On Wed, Dec 31, 2008 at 5:26 PM, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: On Wed, 31 Dec 2008 12:35:20 -0800, Hamish McKenzie wrote: sometimes I want to be able to initialize an instance with a variety of different data types. Type conversion is a bit of a misleading subject line. You're not really converting different types, just initialising from different types. as an obvious example I might want to initialize a 4x4 matrix with either 16 floats, a list/tuple or 16 floats, another matrix or a quaternion. is there any other way to do it other than putting case statements in the __init__ method of the class, or having a Matrix.FromQuaternion( quat )? You could have an external function qtom: def qtom(quaternion): a, b, c, d = quaternion return Matrix([ a, 0, 0, 0, 0, b, 0, 0, 0, 0, c, 0, 0, 0, 0, d]) But the first two solutions seem reasonable to me, except of course Python doesn't have a case statement you have to use an if...elseif block. You could also use a dict with type:method key/value pairings. This is closer to a switch/case than an if...elif chain is. (untested) def __init__(self, *args) : inits = {list: self._init_from_list, float: self._init_from_floats, Matrix: self._init_from_matrix} #and so on inits[type(args[0])](*args) -- Steven -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
KraftDiner wrote: I have the following code... import array len32 = array.array('L') len16 = array.array('H') len32.append(0) len16.append(0) y = len32[0] print y.__class__ type 'long' z = len16[0] print z.__class__ type 'int' how can I change Zs type to long? z_long = long(z) type(z_long) type 'long' Or how how can I change an arrays type? -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
Rob Cowie wrote: KraftDiner wrote: I have the following code... import array len32 = array.array('L') len16 = array.array('H') len32.append(0) len16.append(0) y = len32[0] print y.__class__ type 'long' z = len16[0] print z.__class__ type 'int' how can I change Zs type to long? z_long = long(z) type(z_long) type 'long' Or how how can I change an arrays type? In C++ you can cast one class type to another if you override the operator= Then you can convert one class type to another... In Python it would appear that the left hand side of the assignment operator is not used to determine if a cast is necessary. So how would I do this in python? a = classA() b = classB() b = a In the end b would end up just being a refrence to a no conversion would have been done. -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
KraftDiner wrote: In C++ you can cast one class type to another if you override the operator= Then you can convert one class type to another... In Python it would appear that the left hand side of the assignment operator is not used to determine if a cast is necessary. So how would I do this in python? a = classA() b = classB() b = a In the end b would end up just being a refrence to a (You aren't quite correct there: the name 'b' would refer to the same object as is referred to by the name 'a'. However there is nothing associating the two names 'b' and 'a', so no reference from 'b' to 'a'.) no conversion would have been done. You just have to call the appropriate constructor: b = classB(a) So long as the constructor for classB knows how to create an instance from a classA instance this will have the desired effect. So: aStr = str(anInt) aFloat = float(anInt) ...and so on... -- http://mail.python.org/mailman/listinfo/python-list
Re: Type conversion?
KraftDiner [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] In C++ you can cast one class type to another if you override the operator= Then you can convert one class type to another... In Python it would appear that the left hand side of the assignment operator is not used to determine if a cast is necessary. So how would I do this in python? Start by repeating 50 times: Python is not C++. In Python, '=' is not an operator, it is a name binder, and is defined at the language level, not at the class level. a = classA() b = classB() b = a In the end b would end up just being a refrence to a no conversion would have been done. Just as you say, '=' will completely rebind the rhs value to the lhs name. Now one thing you *could* do is to do something tricky, like use one of the overridable inplace operators, like =, which *is* definable by class. Here is a type casting class that does something like you are asking for. (Back in the CORBA days, = was used in the C++ binding to insert values into a CORBA::Any variable - I guess it looks like some sort of typographic hypodermic needle...) -- Paul class Caster(object): def __init__(self,typ): self.castType = typ self.value = self.castType() def cast(self,other): try: return self.castType(other) except Exception,e: print Failed to cast '%s' as %s % (other, self.castType.__name__) # define behavior for = operator def __ilshift__(self,other): self.value = self.cast(other) return self def __str__(self): return (%s) %s % (self.castType.__name__,self.value) z = Caster(int) print z z = 100 print z z = 3.14159 print z z = 'what the heck?' print z Prints: (int) 0 (int) 100 (int) 3 Failed to cast 'what the heck?' as int (int) None -- http://mail.python.org/mailman/listinfo/python-list