constructor overwrite

Discussion in 'Python' started by Mug, Feb 15, 2010.

  1. Mug

    Mug Guest

    hi ,i had a problem on constructor overwrite:
    i have something like:

    class obj:
    def __init__(self, x=100, y=None):
    if y is None:
    self.x=x
    else:
    self.y=y
    so i can call :
    objet = obj() # x=100 y=None
    or
    objet = obj(40) # x= 40 y=None

    but if i do :
    objet = obj('not cool') #x='not cool' y=None
    since x is not typed .

    i am waiting for a result:
    objet = obj('not cool') #x=100 y='not cool'
    as they do in C++ or java.
    is there a way to do it?
    thanks
    Mug, Feb 15, 2010
    #1
    1. Advertising

  2. Mug <> writes:

    > hi ,i had a problem on constructor overwrite:
    > i have something like:
    >
    > class obj:
    > def __init__(self, x=100, y=None):
    > if y is None:
    > self.x=x
    > else:
    > self.y=y
    > so i can call :
    > objet = obj() # x=100 y=None
    > or
    > objet = obj(40) # x= 40 y=None
    >
    > but if i do :
    > objet = obj('not cool') #x='not cool' y=None
    > since x is not typed .
    >
    > i am waiting for a result:
    > objet = obj('not cool') #x=100 y='not cool'
    > as they do in C++ or java.
    > is there a way to do it?
    > thanks


    Your problem doesn't seem very well defined (e.g. do you ever call obj
    with two arguments?), but as I understand it you can do this:

    def __init__(self, x=100):
    if isinstance(x, int):
    self.x, self.y = x, None
    else:
    self.x, self.y = None, x

    HTH

    --
    Arnaud
    Arnaud Delobelle, Feb 15, 2010
    #2
    1. Advertising

  3. Mug

    Steve Holden Guest

    Mug wrote:
    > hi ,i had a problem on constructor overwrite:
    > i have something like:
    >
    > class obj:
    > def __init__(self, x=100, y=None):
    > if y is None:
    > self.x=x
    > else:
    > self.y=y
    > so i can call :
    > objet = obj() # x=100 y=None
    > or
    > objet = obj(40) # x= 40 y=None
    >
    > but if i do :
    > objet = obj('not cool') #x='not cool' y=None
    > since x is not typed .
    >
    > i am waiting for a result:
    > objet = obj('not cool') #x=100 y='not cool'
    > as they do in C++ or java.
    > is there a way to do it?
    > thanks


    You could check the type(s) of the argument(s) in your code, if you
    want, but Python does not support signature analysis, dynamic method
    dispatch or static typing. You can do

    object = obj("y='not cool')

    but I doubt this is what you want. Your post raises a couple of thier
    points.

    First, __init__ is *not* the constructor. By the time it is called
    creation of the new object is already complete, and __init__() (as its
    name suggests) merely initializes it. In Python 2's "new-style" classes,
    and in Python 3, construction is performed by the class's __new__() method.

    Secondly, it seems a little strange that you are happy to create
    different instances, in some of which self.y is not initialized and in
    others self.x is not initialized. You may have a good reason for doing
    this, I merely point it out as a potential cause of AttributeError
    exceptions.

    regards
    Steve

    regards
    Steve

    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Feb 15, 2010
    #3
  4. Mug a ├ęcrit :
    > hi ,i had a problem on constructor overwrite:
    > i have something like:
    >
    > class obj:
    > def __init__(self, x=100, y=None):
    > if y is None:
    > self.x=x
    > else:
    > self.y=y


    With such an initializer, you'll have instances with an attribute 'y'
    and no attribute 'x', and instances with an attribute 'x' and no
    attribute 'y' :

    >>> class Obj(object):

    .... def __init__(self, x=100, y=None):
    .... if y is None: self.x = x
    .... else: self.y = y
    ....
    >>> objx = Obj()
    >>> objx.x

    100
    >>> objx.y

    Traceback (most recent call last):
    File "<console>", line 1, in <module>
    AttributeError: 'Obj' object has no attribute 'y'
    >>> objy = Obj(y='foo')
    >>> objy.x

    Traceback (most recent call last):
    File "<console>", line 1, in <module>
    AttributeError: 'Obj' object has no attribute 'x'
    >>> objy.y

    'foo'
    >>>



    Are you *sure* this is what you want ?

    > so i can call :
    > objet = obj() # x=100 y=None
    > or
    > objet = obj(40) # x= 40 y=None
    >
    > but if i do :
    > objet = obj('not cool') #x='not cool' y=None


    What else would you expect ???

    > since x is not typed .


    'x' is a name, and names are indeed "untyped". Now the object bound to
    name 'x' is actually typed.

    > i am waiting for a result:
    > objet = obj('not cool') #x=100 y='not cool'
    > as they do in C++ or java.


    Python is neither C++ nor Java (nor Pascal nor Lisp nor
    <yourfavoritelanguagehere> FWIW), so trying to forcefit C++/Java idioms
    will at best lead you to pain and frustation. Just like trying to
    forcefit Python idioms in C++ or Java (or Pascal or Lisp etc....).

    > is there a way to do it?


    objet = obj(y='not cool')

    Now if you could explain the problem you're trying to solve instead of
    the solution you thought would solve it, we might eventually provide
    more help.
    Bruno Desthuilliers, Feb 15, 2010
    #4
    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. =?Utf-8?B?Um9iZXJ0?=

    Deployment in 2.0 - How To Not Overwrite web.config

    =?Utf-8?B?Um9iZXJ0?=, Nov 2, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    4,446
    Juan T. Llibre
    Nov 3, 2005
  2. VisionSet
    Replies:
    1
    Views:
    4,167
    Adam Lipscombe
    Sep 2, 2003
  3. Philipp Leitner

    Overwrite Default Constructor ?

    Philipp Leitner, Apr 17, 2006, in forum: Java
    Replies:
    26
    Views:
    4,399
    Jeroen V.
    Apr 19, 2006
  4. Replies:
    9
    Views:
    473
    Bo Persson
    Jan 5, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
Loading...

Share This Page