Re: return an object of a different class

2011-02-22 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Mon, 21 Feb 2011 14:23:10 +0100, Jean-Michel Pichavant wrote: What is not legit, is to return different objects for which the caller has to test the type to know what attributes he can use. Well, I don't know... I'm of two minds. On the one hand, I find it

Re: return an object of a different class

2011-02-21 Thread Jean-Michel Pichavant
alex23 wrote: Jean-Michel Pichavant jeanmic...@sequans.com wrote: You simply don't return inconsistent types with a return statement. This is a general rule in programming that has probably exceptions but regarding what you're saying, you clearly don't want to do that. I don't think

Re: return an object of a different class

2011-02-21 Thread Steven D'Aprano
On Mon, 21 Feb 2011 14:23:10 +0100, Jean-Michel Pichavant wrote: What is not legit, is to return different objects for which the caller has to test the type to know what attributes he can use. Well, I don't know... I'm of two minds. On the one hand, I find it *really annoying* when this

Re: return an object of a different class

2011-02-19 Thread Piet van Oostrum
Richard Thomas chards...@gmail.com writes: If you don't want to use a factory function I believe you can do this: class MyNumber(object): def __new__(cls, n): if n = 100: cls = SmallNumbers else: cls = BigNumbers return

Re: return an object of a different class

2011-02-17 Thread Jean-Michel Pichavant
s...@uce.gov wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n = 100: self = SmallNumers(self) else: self = BigNumbers(self) class SmallNumbers: def __init__(self, n):

Re: return an object of a different class

2011-02-17 Thread Jean-Michel Pichavant
Karim wrote: [snip] If you don't want to use a factory function I believe you can do this: class MyNumber(object): def __new__(cls, n): if n= 100: cls = SmallNumbers else: cls = BigNumbers return object.__new__(cls, n) ... Chard.

Re: return an object of a different class

2011-02-17 Thread Steven D'Aprano
On Thu, 17 Feb 2011 12:02:28 +0100, Jean-Michel Pichavant wrote: Karim wrote: [snip] If you don't want to use a factory function I believe you can do this: class MyNumber(object): def __new__(cls, n): if n= 100: cls = SmallNumbers else:

Re: return an object of a different class

2011-02-17 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Thu, 17 Feb 2011 12:02:28 +0100, Jean-Michel Pichavant wrote: Karim wrote: [snip] If you don't want to use a factory function I believe you can do this: class MyNumber(object): def __new__(cls, n): if n= 100: cls =

Re: return an object of a different class

2011-02-17 Thread Westley Martínez
On Thu, 2011-02-17 at 11:43 +, Steven D'Aprano wrote: On Thu, 17 Feb 2011 12:02:28 +0100, Jean-Michel Pichavant wrote: Karim wrote: [snip] If you don't want to use a factory function I believe you can do this: class MyNumber(object): def __new__(cls, n): if n=

Re: return an object of a different class

2011-02-17 Thread MRAB
On 17/02/2011 14:39, Westley Martínez wrote: On Thu, 2011-02-17 at 11:43 +, Steven D'Aprano wrote: On Thu, 17 Feb 2011 12:02:28 +0100, Jean-Michel Pichavant wrote: Karim wrote: [snip] If you don't want to use a factory function I believe you can do this: class MyNumber(object):

Re: return an object of a different class

2011-02-17 Thread Westley Martínez
On Thu, 2011-02-17 at 15:56 +, MRAB wrote: On 17/02/2011 14:39, Westley Martínez wrote: On Thu, 2011-02-17 at 11:43 +, Steven D'Aprano wrote: On Thu, 17 Feb 2011 12:02:28 +0100, Jean-Michel Pichavant wrote: Karim wrote: [snip] If you don't want to use a factory function I

Re: return an object of a different class

2011-02-17 Thread alex23
Jean-Michel Pichavant jeanmic...@sequans.com wrote: You simply don't return inconsistent types with a return statement. This is a general rule in programming that has probably exceptions but regarding what you're saying, you clearly don't want to do that. I don't think they were intended to be

Re: return an object of a different class

2011-02-17 Thread Steven D'Aprano
On Thu, 17 Feb 2011 15:53:14 -0800, Westley Martínez wrote: Python 3 removed longs because they were ... cryptonic! Strictly speaking, they weren't removed. ints were removed and long was renamed int. My point stands. Your point is wrong. Ints and longs weren't unified because it is

Re: return an object of a different class

2011-02-17 Thread Westley Martínez
On Fri, 2011-02-18 at 02:25 +, Steven D'Aprano wrote: On Thu, 17 Feb 2011 15:53:14 -0800, Westley Martínez wrote: Python 3 removed longs because they were ... cryptonic! Strictly speaking, they weren't removed. ints were removed and long was renamed int. My point stands. Your

Re: return an object of a different class

2011-02-16 Thread Karim
On 02/16/2011 06:05 AM, Richard Thomas wrote: On Feb 16, 2:23 am, s...@uce.gov wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n= 100: self = SmallNumers(self) else:

Re: return an object of a different class

2011-02-16 Thread Steven D'Aprano
On Tue, 15 Feb 2011 19:23:39 -0700, spam wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n = 100: self = SmallNumers(self) else: self = BigNumbers(self) (1)

Re: return an object of a different class

2011-02-16 Thread Steven D'Aprano
On Tue, 15 Feb 2011 22:17:13 -0700, spam wrote: I didn't explain my problem, chose a terrible example. This is more what I'm trying to do: [snip thingy class] No, your first example was better. This one is terrible -- it's so generic it's meaningless. In any case, you don't explain why it

return an object of a different class

2011-02-15 Thread spam
How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n = 100: self = SmallNumers(self) else: self = BigNumbers(self) class SmallNumbers: def __init__(self, n): self.size = 'small'

Re: return an object of a different class

2011-02-15 Thread MRAB
On 16/02/2011 02:23, s...@uce.gov wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n = 100: self = SmallNumers(self) else: self = BigNumbers(self) class SmallNumbers: def

Re: return an object of a different class

2011-02-15 Thread Ben Finney
s...@uce.gov writes: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n = 100: self = SmallNumers(self) else: self = BigNumbers(self) A class defines a type of object. If you

Re: return an object of a different class

2011-02-15 Thread alex23
On Feb 16, 12:23 pm, s...@uce.gov wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers:    def __init__(self, n):      self.original_value = n      if n = 100:        self = SmallNumers(self)      else:        self = BigNumbers(self) class

Re: return an object of a different class

2011-02-15 Thread alex23
alex23 wuwe...@gmail.com wrote:     self.__class__ = BigNumbers if n 100 else SmallThing That should, of course, be SmallNumbers :) -- http://mail.python.org/mailman/listinfo/python-list

Re: return an object of a different class

2011-02-15 Thread aitilang
er I think you need a NumberFactory that makes SmallNumber and BigNumber according to the initial value. ?? 2011-2-16 10:23, s...@uce.gov : How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value

Re: return an object of a different class

2011-02-15 Thread Richard Thomas
On Feb 16, 2:23 am, s...@uce.gov wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers:    def __init__(self, n):      self.original_value = n      if n = 100:        self = SmallNumers(self)      else:        self = BigNumbers(self) class

Re: return an object of a different class

2011-02-15 Thread spam
On 11-02-15 07:45 PM, alex23 wrote: Firstly, does MyNumbers _have_ to be a class? Or would a function acting as a class factory be sufficient? Yes it does. I didn't explain my problem, chose a terrible example. This is more what I'm trying to do: class thingy: def __init__(self,

Re: return an object of a different class

2011-02-15 Thread spam
I didn't explain my problem, chose a terrible example. This is more what I'm trying to do: class thingy: def __init__(self, athingy): self.basic_extract() if self.typeof = A .../... def basic_extract(self): # complicated logic to extract data out of the thingy here

Re: return an object of a different class

2011-02-15 Thread Ben Finney
s...@uce.gov writes: I didn't explain my problem, chose a terrible example. This is more what I'm trying to do: Unfortunately, it's still very contrived, and the names don't give any suggestion as to what you're trying to achieve. Can you improve on that? class ThingyTypeA: def

Re: return an object of a different class

2011-02-15 Thread spam
I didn't explain my problem, chose a terrible example. This is more what I'm trying to do: Basically the subclass I want to use is based on some of the data I extract from a blob of data. If I use a function to extract the data before I create the objects, then I need to do a bunch of

Re: return an object of a different class

2011-02-15 Thread Ben Finney
s...@uce.gov writes: Perhaps you want those classes to inherit from your base class. Have you done the Python tutorial? It covers inheritance and how to use it. Yes, I have done subclasing before, and yes ThingyTypeA and B should be subclassing Thingy Then your example still gives no