FAQ
I'm writing a class for rational numbers
and besides the most obvious constructor

def __init__ (self, nomin, denom):

i also wish to have two supporting ones

def __init__ (self, integ):
self.__init__ (integ, 1)
def __init__ (self):
self.__init__ (0, 1)

but for some reason (not known to me at
this point) i get errors. My suspicion
is that it's a syntax issue.

Suggestions?

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Search Discussions

  • Diez B. Roggisch at Mar 3, 2008 at 8:09 pm

    K Viltersten schrieb:
    I'm writing a class for rational numbers
    and besides the most obvious constructor

    def __init__ (self, nomin, denom):

    i also wish to have two supporting ones

    def __init__ (self, integ):
    self.__init__ (integ, 1)
    def __init__ (self):
    self.__init__ (0, 1)

    but for some reason (not known to me at
    this point) i get errors. My suspicion is that it's a syntax issue.

    Suggestions?
    "errors" is not much of an error-description. That's what stacktraces
    are for.

    Apart from that, you won't succeed with the above. Python has no
    signature-based polymorphism. Instead, you use default arguments, like this:


    def __init__(nomin=0, denom=1):
    ...

    That should suffice.

    Diez
  • K Viltersten at Mar 4, 2008 at 8:05 pm
    "Diez B. Roggisch" <deets at nospam.web.de> skrev i meddelandet
    news:63346uF25gigqU1 at mid.uni-berlin.de...
    K Viltersten schrieb:
    I'm writing a class for rational numbers
    and besides the most obvious constructor

    def __init__ (self, nomin, denom):

    i also wish to have two supporting ones

    def __init__ (self, integ):
    self.__init__ (integ, 1)
    def __init__ (self):
    self.__init__ (0, 1)

    but for some reason (not known to me at
    this point) i get errors. My suspicion is that it's a syntax issue.
    "errors" is not much of an error-description. That's what stacktraces are
    for.
    I assumed that the error was so obvious to a
    seasoned Pytonist (Pythoner?) that a trace
    didn't matter. Your help below proves it. :)

    Nevertheless, i'll be careful in the future
    and make sure to post the traces too. Sorry.
    Apart from that, you won't succeed with the above. Python has no
    signature-based polymorphism. Instead, you use default arguments, like
    this:

    def __init__(nomin=0, denom=1):
    ...
    Thank you.

    --
    Regards
    Konrad Viltersten
    --------------------------------
    sleep - a substitute for coffee for the poor
    ambition - lack of sense to be lazy
  • Jeff Schwab at Mar 4, 2008 at 9:53 pm
    What does "SV" in the subject mean?
  • Tommy Grav at Mar 5, 2008 at 1:06 am

    On Mar 4, 2008, at 4:53 PM, Jeff Schwab wrote:

    What does "SV" in the subject mean?
    SV = "Svar" is the Norwegian word for Reply.

    Cheers
    Tommy
  • Castironpi at Mar 5, 2008 at 2:31 am

    On Mar 4, 7:06?pm, Tommy Grav wrote:
    On Mar 4, 2008, at 4:53 PM, Jeff Schwab wrote:

    What does "SV" in the subject mean?
    SV = "Svar" is the Norwegian word for Reply.

    Cheers
    ? ?Tommy
    It is also the name of my lockermate in grade school. "So, Svar, how
    'bout them posters?"
  • Jeff Schwab at Mar 5, 2008 at 4:04 am

    Tommy Grav wrote:
    On Mar 4, 2008, at 4:53 PM, Jeff Schwab wrote:

    What does "SV" in the subject mean?
    SV = "Svar" is the Norwegian word for Reply.
    Thanks. Serves me right for not speaking Norwegian.
  • K Viltersten at Mar 5, 2008 at 4:26 am
    What does "SV" in the subject mean?

    Probably, it's an abbreviation of
    "svar", which means "reply".

    --
    Regards
    Konrad Viltersten
    --------------------------------
    sleep - a substitute for coffee for the poor
    ambition - lack of sense to be lazy
  • Jeff Schwab at Mar 5, 2008 at 4:26 pm

    Dennis Lee Bieber wrote:
    On Tue, 4 Mar 2008 20:06:38 -0500, Tommy Grav <tgrav at mac.com> declaimed
    the following in comp.lang.python:
    SV = "Svar" is the Norwegian word for Reply.
    Ah, good... In my working life, "SV" => "Space Vehicle", often used
    to differentiate between the base satellite and "PL" Payload (the part
    that earns the money)
    Which is which? Aren't those both part of the space vehicle? Btw, do
    you work for government or industry? Do you enjoy working with the
    space program? I've heard only indirect reviews, and they've been mixed.

    [1] "Av Emne," according to the free online translater.
    http://www.tranexp.com:2000/Translate/result.shtml
  • Jeff Schwab at Mar 6, 2008 at 5:16 am

    Dennis Lee Bieber wrote:
    On Wed, 05 Mar 2008 08:26:04 -0800, Jeff Schwab <jeff at schwabcenter.com>
    declaimed the following in comp.lang.python:
    Which is which? Aren't those both part of the space vehicle? Btw, do
    you work for government or industry? Do you enjoy working with the
    space program? I've heard only indirect reviews, and they've been mixed.
    Lockheed... I don't really work with the sat's themselves.

    When it comes to division of responsibility, the "payload" is the
    part the customer wants in orbit. The "space vehicle" is the part that
    carries it around in that orbit -- the SV has the station-keeping
    thrusters (I'm presuming a geosynchronous orbit), the power supply and
    solar panels... The payload may just consist of a wide-band transponder
    (for direct TV, say) and maybe an independent commanding system (its own
    receive transmit gear on a different frequency from the satellite
    control -- though I wouldn't really expect this type of split)
    Interesting. Thanks.
  • Raymond Hettinger at Mar 3, 2008 at 9:17 pm

    On Mar 3, 12:21 pm, "K Viltersten" wrote:
    I'm writing a class for rational numbers
    and besides the most obvious constructor

    def __init__ (self, nomin, denom):

    i also wish to have two supporting ones

    def __init__ (self, integ):
    self.__init__ (integ, 1)
    def __init__ (self):
    self.__init__ (0, 1)
    For this particular use case, providing default arguments will
    suffice:

    class Fraction:
    def __init__(self, numerator=0, denomiator=1):
    ...

    Since Python doesn't support having two methods with the same name,
    the usual solution is to provide alternative constructors using
    classmethod():

    @classmethod
    def from_decimal(cls, d)
    sign, digits, exp = d.as_tuple()
    digits = int(''.join(map(str, digits)))
    if sign:
    digits = -digits
    if exp >= 0:
    return cls(digits * 10 ** exp)
    return cls(digits, 10 ** -exp)


    Raymond
  • Carl Banks at Mar 3, 2008 at 10:03 pm

    On Mar 3, 4:17 pm, Raymond Hettinger wrote:
    Since Python doesn't support having two methods with the same name,
    the usual solution is to provide alternative constructors using
    classmethod():

    @classmethod
    def from_decimal(cls, d)
    sign, digits, exp = d.as_tuple()
    digits = int(''.join(map(str, digits)))
    if sign:
    digits = -digits
    if exp >= 0:
    return cls(digits * 10 ** exp)
    return cls(digits, 10 ** -exp)

    Note that even some of Python's built in types (dict *cough*)
    implement homemade function overloading.

    The OP wanted to write a constructor that could accept either a pair
    of integers or a rational, there would be a good precedent for it.

    However, I would advise the OP to use the constructor only for the
    most common arguments, and use classmethods for more obscure, less
    common arguments (such as decimal or even float).


    Carl Banks
  • Castironpi at Mar 4, 2008 at 3:21 am

    On Mar 3, 4:03?pm, Carl Banks wrote:
    On Mar 3, 4:17 pm, Raymond Hettinger wrote:

    Since Python doesn't support having two methods with the same name,
    the usual solution is to provide alternative constructors using
    classmethod():
    ? @classmethod
    ? def from_decimal(cls, d)
    ? ? ? ? sign, digits, exp = d.as_tuple()
    ? ? ? ? digits = int(''.join(map(str, digits)))
    ? ? ? ? if sign:
    ? ? ? ? ? ? digits = -digits
    ? ? ? ? if exp >= 0:
    ? ? ? ? ? ? return cls(digits * 10 ** exp)
    ? ? ? ? return cls(digits, 10 ** -exp)
    Note that even some of Python's built in types (dict *cough*)
    implement homemade function overloading.

    The OP wanted to write a constructor that could accept either a pair
    of integers or a rational, there would be a good precedent for it.

    However, I would advise the OP to use the constructor only for the
    most common arguments, and use classmethods for more obscure, less
    common arguments (such as decimal or even float).

    Carl Banks
    Unless application indicates calling signature uniformity, hash type
    to subclass or to class method.
  • K Viltersten at Mar 4, 2008 at 8:00 pm
    "Carl Banks" <pavlovevidence at gmail.com> skrev i meddelandet
    news:24adaf72-0c55-4a88-86d0-296363ead17f at s8g2000prg.googlegroups.com...
    On Mar 3, 4:17 pm, Raymond Hettinger wrote:
    Since Python doesn't support having two methods with the same name,
    the usual solution is to provide alternative constructors using
    classmethod():

    @classmethod
    def from_decimal(cls, d)
    sign, digits, exp = d.as_tuple()
    digits = int(''.join(map(str, digits)))
    if sign:
    digits = -digits
    if exp >= 0:
    return cls(digits * 10 ** exp)
    return cls(digits, 10 ** -exp)

    Note that even some of Python's built in types (dict *cough*)
    implement homemade function overloading.

    The OP wanted to write a constructor that could accept either a pair
    of integers or a rational, there would be a good precedent for it.

    However, I would advise the OP to use the constructor only for the
    most common arguments, and use classmethods for more obscure, less
    common arguments (such as decimal or even float).

    OP understands and thanfully accepts
    the suggestion.

    --
    Regards
    Konrad Viltersten
    --------------------------------
    sleep - a substitute for coffee for the poor
    ambition - lack of sense to be lazy

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 3, '08 at 8:09p
activeMar 6, '08 at 5:16a
posts14
users7
websitepython.org

People

Translate

site design / logo © 2023 Grokbase