return an object of a different class

Discussion in 'Python' started by spam@uce.gov, Feb 16, 2011.

  1. Guest

    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'

    class BigNumbers:
    def __init__(self, n):
    self.size = 'big'

    t = MyNumbers(200)


    When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers,
    because BigNumbers and SmallNumbers will have different methods etc...

    Do I need to use metaclasses?

    Thanks.
    --
    Yves. http://www.SollerS.ca/
    http://blog.zioup.org/
     
    , Feb 16, 2011
    #1
    1. Advertising

  2. MRAB Guest

    On 16/02/2011 02:23, 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):
    > self.size = 'small'
    >
    > class BigNumbers:
    > def __init__(self, n):
    > self.size = 'big'
    >
    > t = MyNumbers(200)
    >
    >
    > When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers,
    > because BigNumbers and SmallNumbers will have different methods etc...
    >
    > Do I need to use metaclasses?
    >

    Why not just make MyNumbers a function?
     
    MRAB, Feb 16, 2011
    #2
    1. Advertising

  3. aitilang Guest

    er................
    I think you need a NumberFactory that makes SmallNumber and BigNumber
    according to the initial value.

    ÓÚ 2011-2-16 10:23, дµÀ:
    > 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'
    >
    > class BigNumbers:
    > def __init__(self, n):
    > self.size = 'big'
    >
    > t = MyNumbers(200)
    >
    >
    > When I do type(t) it says MyNumbers, while I'd want it to be
    > BigNumbers, because BigNumbers and SmallNumbers will have different
    > methods etc...
    >
    > Do I need to use metaclasses?
    >
    > Thanks.
     
    aitilang, Feb 16, 2011
    #3
  4. alex23 Guest

    On Feb 16, 12:23 pm, 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):
    >      self.size = 'small'
    >
    > class BigNumbers:
    >    def __init__(self, n):
    >      self.size = 'big'
    >
    > t = MyNumbers(200)
    >
    > When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers,
    > because BigNumbers and SmallNumbers will have different methods etc...


    Firstly, does MyNumbers _have_ to be a class? Or would a function
    acting as a class factory be sufficient?

    Otherwise, you can change the class of an object, even within its own
    methods:

    class MyNumbers(object):
    def __init__(self, n = 0):
    self.original_value = n
    self.__class__ = BigNumbers if n > 100 else SmallThing

    class BigNumbers(MyNumbers):
    size = 'big'

    class SmallNumbers(MyNumbers):
    size = 'small'

    >>> from test import *
    >>> s = MyNumbers(50)
    >>> b = MyNumbers(200)
    >>> type(s)

    <class 'test.SmallNumbers'>
    >>> s.size

    'small'
    >>> type(b)

    <class 'test.BigNumbers'>
    >>> b.size

    'big'

    Hope this helps.
     
    alex23, Feb 16, 2011
    #4
  5. alex23 Guest

    alex23 <> wrote:
    >     self.__class__ = BigNumbers if n > 100 else SmallThing


    That should, of course, be SmallNumbers :)
     
    alex23, Feb 16, 2011
    #5
  6. On Feb 16, 2:23 am, 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):
    >      self.size = 'small'
    >
    > class BigNumbers:
    >    def __init__(self, n):
    >      self.size = 'big'
    >
    > t = MyNumbers(200)
    >
    > When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers,
    > because BigNumbers and SmallNumbers will have different methods etc...
    >
    > Do I need to use metaclasses?
    >
    > Thanks.
    > --
    > Yves.                                                  http://www.SollerS.ca/
    >                                                        http://blog.zioup.org/


    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.
     
    Richard Thomas, Feb 16, 2011
    #6
  7. Guest

    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, athingy):
    self.basic_extract()
    if self.sortof =

    def basic_extract(self):
    do a bunch of things
    self.sortof = ..

    def general_method(self)


    class ThingyTypeA:
    def __init__(self):
    further_extract()

    class ThingyTypeB:
    def __init__(self):
    further_extract()

    >
    > Otherwise, you can change the class of an object, even within its own
    > methods:


    And then I just call __init__ method?

    >
    > class MyNumbers(object):
    > def __init__(self, n = 0):
    > self.original_value = n
    > self.__class__ = BigNumbers if n> 100 else SmallThing

    self.__init__()
    >
    > class BigNumbers(MyNumbers):

    def __init__(self):
    > size = 'big'

    self.newvalue = self.original_value * y

    >
    > class SmallNumbers(MyNumbers):

    def __init__(self):
    > size = 'small'

    self.newvalue = self.original_value * x

    >
    > Hope this helps.


    Yes, thanks!


    --
    Yves. http://www.SollerS.ca/
    http://blog.zioup.org/
     
    , Feb 16, 2011
    #7
  8. Guest

    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
    # and set a bunch of values base on the data we extract
    self.size = xxx
    self.typeof = yyy
    self.weight = zzz

    def general_method(self)
    # Method that can be used on both types of thingy

    class ThingyTypeA:
    def __init__(self):
    # do some further extraction specific to type A
    further_extract()


    class ThingyTypeB:
    def __init__(self):
    # do some further extraction specific to type B
    further_extract()


    --
    Yves. http://www.SollerS.ca/
    http://blog.zioup.org/
     
    , Feb 16, 2011
    #8
  9. Guest

    >> 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 assignments afterwards.

    >
    > 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 __init__(self):
    >> # do some further extraction specific to type A
    >> further_extract()
    >>
    >>
    >> class ThingyTypeB:
    >> def __init__(self):
    >> # do some further extraction specific to type B
    >> further_extract()

    >
    > 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



    --
    Yves. http://www.SollerS.ca/
    http://blog.zioup.org/
     
    , Feb 16, 2011
    #9
  10. Karim Guest

    On 02/16/2011 06:05 AM, Richard Thomas wrote:
    > On Feb 16, 2:23 am, 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):
    >> self.size = 'small'
    >>
    >> class BigNumbers:
    >> def __init__(self, n):
    >> self.size = 'big'
    >>
    >> t = MyNumbers(200)
    >>
    >> When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers,
    >> because BigNumbers and SmallNumbers will have different methods etc...
    >>
    >> Do I need to use metaclasses?
    >>
    >> Thanks.
    >> --
    >> Yves. http://www.SollerS.ca/
    >> http://blog.zioup.org/

    > 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.


    Very beautiful code great alternative to factory method!
    To memorize this pythonic way.

    Regards
    Karim
     
    Karim, Feb 16, 2011
    #10
  11. 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) self is just a local variable, it isn't privileged in any way.
    Assigning to the name "self" doesn't magically change the instance, so
    that can't work.

    (2) By the time the __init__ method is called, the instance has been
    created. __init__ is the initializer, you need the constructor. Something
    like this:


    # Untested.
    class MyNumbers:
    def __new__(cls, n):
    if n <= 100:
    instance = SmallNumbers(n)
    else:
    instance = BigNumbers(n)
    return instance



    --
    Steven
     
    Steven D'Aprano, Feb 16, 2011
    #11
  12. 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 has to be a class, rather than a
    factory function.


    --
    Steven
     
    Steven D'Aprano, Feb 16, 2011
    #12
  13. 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):
    > self.size = 'small'
    >
    > class BigNumbers:
    > def __init__(self, n):
    > self.size = 'big'
    >
    > t = MyNumbers(200)
    >
    >
    > When I do type(t) it says MyNumbers, while I'd want it to be
    > BigNumbers, because BigNumbers and SmallNumbers will have different
    > methods etc...
    >
    > Do I need to use metaclasses?
    >
    > Thanks.

    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.

    Immagine the following code:

    oNumber = MyNumbers(random.int(100)) # note that oNumber is not a
    MyNumbers instance... quite confusing don't you think ?
    oNumber. ... wait a minute, which methods am I allowed to call ???
    SmallNumbers adn BigNumbers have differents methods.

    Some rules to follow, until you get some experience with python:
    * use consistent types for method parameters.
    * use consistent types for returned values.
    * a class constructor should return an instance of that very same class.

    JM
     
    Jean-Michel Pichavant, Feb 17, 2011
    #13
  14. 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.

    >
    > Very beautiful code great alternative to factory method!
    > To memorize this pythonic way.
    >
    > Regards
    > Karim

    Do you think that the MyNumber constructor returning something else
    than a MyNumber instance is the pythonic way ? It would rather be the
    cryptonic way ! (haha)

    JM
     
    Jean-Michel Pichavant, Feb 17, 2011
    #14
  15. 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:
    >>> cls = BigNumbers
    >>> return object.__new__(cls, n)
    >>> ...
    >>>
    >>> Chard.

    >>
    >> Very beautiful code great alternative to factory method! To memorize
    >> this pythonic way.
    >>
    >> Regards
    >> Karim

    > Do you think that the MyNumber constructor returning something else
    > than a MyNumber instance is the pythonic way ? It would rather be the
    > cryptonic way ! (haha)



    Support for constructors returning something other than an instance of
    the class is not an accident, it is a deliberate, and useful, design. The
    Fine Manual says:

    object.__new__(cls[, ...])

    Called to create a new instance of class cls. [...]
    The return value of __new__() should be the new object
    instance (usually an instance of cls).
    [...]
    If __new__() does not return an instance of cls, then
    the new instance’s __init__() method will not be invoked.


    http://docs.python.org/reference/datamodel.html#basic-customization

    So while it is *usual* for the constructor to return an instance of the
    class, it's not compulsory, and returning other types is explicitly
    supported.

    To answer your question about whether this is Pythonic... here's a small
    clue from Python 2.5:

    >>> n = int("4294967296") # 2**32
    >>> type(n)

    <type 'long'>



    So, yes, absolutely, it is not only allowed for class constructors to
    return an instance of a different class, but there is precedence in the
    built-ins.



    --
    Steven
     
    Steven D'Aprano, Feb 17, 2011
    #15
  16. 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 = SmallNumbers
    >>>> else:
    >>>> cls = BigNumbers
    >>>> return object.__new__(cls, n)
    >>>> ...
    >>>>
    >>>> Chard.
    >>>>
    >>> Very beautiful code great alternative to factory method! To memorize
    >>> this pythonic way.
    >>>
    >>> Regards
    >>> Karim
    >>>

    >> Do you think that the MyNumber constructor returning something else
    >> than a MyNumber instance is the pythonic way ? It would rather be the
    >> cryptonic way ! (haha)
    >>

    >
    >
    > Support for constructors returning something other than an instance of
    > the class is not an accident, it is a deliberate, and useful, design. The
    > Fine Manual says:
    >
    > object.__new__(cls[, ...])
    >
    > Called to create a new instance of class cls. [...]
    > The return value of __new__() should be the new object
    > instance (usually an instance of cls).
    > [...]
    > If __new__() does not return an instance of cls, then
    > the new instance’s __init__() method will not be invoked.
    >
    >
    > http://docs.python.org/reference/datamodel.html#basic-customization
    >
    > So while it is *usual* for the constructor to return an instance of the
    > class, it's not compulsory, and returning other types is explicitly
    > supported.
    >
    > To answer your question about whether this is Pythonic... here's a small
    > clue from Python 2.5:
    >
    >
    >>>> n = int("4294967296") # 2**32
    >>>> type(n)
    >>>>

    > <type 'long'>
    >
    >
    >
    > So, yes, absolutely, it is not only allowed for class constructors to
    > return an instance of a different class, but there is precedence in the
    > built-ins.
    >
    >

    Returning another class instance in a new operator is legit, but
    regarding the documentaiton you quoted, it's also "unusual", for which
    is used the word "cryptonic" instead. But that was only in the attempt
    of being clever. But I'm used to that when it comes to joking in english
    :-/.

    Thing is, I said the same thing that the __new__ documentation.

    Concerning the example you've given, you shoud notice that int and long
    objects have the exact same interface, same attributes. And all their
    methods mean to do the same thing. In fact it doesn't really matter if
    you're manipulating an int or a long, you're still using the 2 objects
    the same way for the same result. From a duck typing POV, they're the
    same class.
    This is not what would happen for the OP, given the examples he gave.

    JM
     
    Jean-Michel Pichavant, Feb 17, 2011
    #16
  17. On Thu, 2011-02-17 at 11:43 +0000, 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 = SmallNumbers
    > >>> else:
    > >>> cls = BigNumbers
    > >>> return object.__new__(cls, n)
    > >>> ...
    > >>>
    > >>> Chard.
    > >>
    > >> Very beautiful code great alternative to factory method! To memorize
    > >> this pythonic way.
    > >>
    > >> Regards
    > >> Karim

    > > Do you think that the MyNumber constructor returning something else
    > > than a MyNumber instance is the pythonic way ? It would rather be the
    > > cryptonic way ! (haha)

    >
    >
    > Support for constructors returning something other than an instance of
    > the class is not an accident, it is a deliberate, and useful, design. The
    > Fine Manual says:
    >
    > object.__new__(cls[, ...])
    >
    > Called to create a new instance of class cls. [...]
    > The return value of __new__() should be the new object
    > instance (usually an instance of cls).
    > [...]
    > If __new__() does not return an instance of cls, then
    > the new instance’s __init__() method will not be invoked.
    >
    >
    > http://docs.python.org/reference/datamodel.html#basic-customization
    >
    > So while it is *usual* for the constructor to return an instance of the
    > class, it's not compulsory, and returning other types is explicitly
    > supported.
    >
    > To answer your question about whether this is Pythonic... here's a small
    > clue from Python 2.5:
    >
    > >>> n = int("4294967296") # 2**32
    > >>> type(n)

    > <type 'long'>
    >
    >
    >
    > So, yes, absolutely, it is not only allowed for class constructors to
    > return an instance of a different class, but there is precedence in the
    > built-ins.
    >
    >
    >
    > --
    > Steven

    Python 3 removed longs because they were ... cryptonic!
     
    Westley Martínez, Feb 17, 2011
    #17
  18. MRAB Guest

    On 17/02/2011 14:39, Westley Martínez wrote:
    > On Thu, 2011-02-17 at 11:43 +0000, 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 = SmallNumbers
    >>>>> else:
    >>>>> cls = BigNumbers
    >>>>> return object.__new__(cls, n)
    >>>>> ...
    >>>>>
    >>>>> Chard.
    >>>>
    >>>> Very beautiful code great alternative to factory method! To memorize
    >>>> this pythonic way.
    >>>>
    >>>> Regards
    >>>> Karim
    >>> Do you think that the MyNumber constructor returning something else
    >>> than a MyNumber instance is the pythonic way ? It would rather be the
    >>> cryptonic way ! (haha)

    >>
    >>
    >> Support for constructors returning something other than an instance of
    >> the class is not an accident, it is a deliberate, and useful, design. The
    >> Fine Manual says:
    >>
    >> object.__new__(cls[, ...])
    >>
    >> Called to create a new instance of class cls. [...]
    >> The return value of __new__() should be the new object
    >> instance (usually an instance of cls).
    >> [...]
    >> If __new__() does not return an instance of cls, then
    >> the new instance’s __init__() method will not be invoked.
    >>
    >>
    >> http://docs.python.org/reference/datamodel.html#basic-customization
    >>
    >> So while it is *usual* for the constructor to return an instance of the
    >> class, it's not compulsory, and returning other types is explicitly
    >> supported.
    >>
    >> To answer your question about whether this is Pythonic... here's a small
    >> clue from Python 2.5:
    >>
    >>>>> n = int("4294967296") # 2**32
    >>>>> type(n)

    >> <type 'long'>
    >>
    >>
    >>
    >> So, yes, absolutely, it is not only allowed for class constructors to
    >> return an instance of a different class, but there is precedence in the
    >> built-ins.
    >>
    >>
    >>
    >> --
    >> Steven

    > Python 3 removed longs because they were ... cryptonic!
    >

    Strictly speaking, they weren't removed. ints were removed and long was
    renamed int.
     
    MRAB, Feb 17, 2011
    #18
  19. On Thu, 2011-02-17 at 15:56 +0000, MRAB wrote:
    > On 17/02/2011 14:39, Westley Martínez wrote:
    > > On Thu, 2011-02-17 at 11:43 +0000, 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 = SmallNumbers
    > >>>>> else:
    > >>>>> cls = BigNumbers
    > >>>>> return object.__new__(cls, n)
    > >>>>> ...
    > >>>>>
    > >>>>> Chard.
    > >>>>
    > >>>> Very beautiful code great alternative to factory method! To memorize
    > >>>> this pythonic way.
    > >>>>
    > >>>> Regards
    > >>>> Karim
    > >>> Do you think that the MyNumber constructor returning something else
    > >>> than a MyNumber instance is the pythonic way ? It would rather be the
    > >>> cryptonic way ! (haha)
    > >>
    > >>
    > >> Support for constructors returning something other than an instance of
    > >> the class is not an accident, it is a deliberate, and useful, design. The
    > >> Fine Manual says:
    > >>
    > >> object.__new__(cls[, ...])
    > >>
    > >> Called to create a new instance of class cls. [...]
    > >> The return value of __new__() should be the new object
    > >> instance (usually an instance of cls).
    > >> [...]
    > >> If __new__() does not return an instance of cls, then
    > >> the new instance’s __init__() method will not be invoked.
    > >>
    > >>
    > >> http://docs.python.org/reference/datamodel.html#basic-customization
    > >>
    > >> So while it is *usual* for the constructor to return an instance of the
    > >> class, it's not compulsory, and returning other types is explicitly
    > >> supported.
    > >>
    > >> To answer your question about whether this is Pythonic... here's a small
    > >> clue from Python 2.5:
    > >>
    > >>>>> n = int("4294967296") # 2**32
    > >>>>> type(n)
    > >> <type 'long'>
    > >>
    > >>
    > >>
    > >> So, yes, absolutely, it is not only allowed for class constructors to
    > >> return an instance of a different class, but there is precedence in the
    > >> built-ins.
    > >>
    > >>
    > >>
    > >> --
    > >> Steven

    > > 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.
     
    Westley Martínez, Feb 17, 2011
    #19
  20. alex23 Guest

    Jean-Michel Pichavant <> 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 inconsistent types, but
    subclasses of the same type. Returning different subclasses is exactly
    what a factory is for. And given Python's propensity for duck typing,
    they don't even really need to be subclassed for the same object, they
    just need the interface that is required.

    > Immagine the following code:
    >
    > oNumber = MyNumbers(random.int(100)) # note that oNumber is not a
    > MyNumbers instance... quite confusing don't you think ?
    > oNumber. ... wait a minute, which methods am I allowed to call ???
    > SmallNumbers adn BigNumbers have differents methods.


    What if they have different implementations rather than methods?
     
    alex23, Feb 18, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  2. Greenhorn
    Replies:
    15
    Views:
    833
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    1
    Views:
    330
    John Harrison
    Feb 21, 2007
  4. Simbad

    Object property with different return types

    Simbad, Jan 12, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    117
    Teemu Keiski
    Jan 18, 2004
  5. Xeno Campanoli
    Replies:
    0
    Views:
    229
    Xeno Campanoli
    Feb 13, 2006
Loading...

Share This Page