a new object definition

Discussion in 'Python' started by Sylvain Ferriol, Sep 1, 2006.

  1. hello everybody,

    i want to talk with you about a question i have in mind and i do not
    find a answer. it 's simple:
    why do we not have a beatiful syntax for object definition as we have
    for class definition ?

    we can define a class in python in 2 ways:
    1. by using the metaclass constructor
    my_class = MyMetaClass(....)
    2. by using the classic definition syntax:
    class my_class(object):
    __metaclass__ = MyMetaClass

    if i want to instanciate an object, i only have one way to define it:
    my_obj = my_class(....)

    why not a better syntax like class syntax ?

    example:
    >> instance my_obj:
    >> __class__ = my_class



    with this syntax, we are coherent between objects and classes.
    why do we have a specific syntax for the class object definition and not
    for objects of different type?

    so if i want to define a new class object (my_class for example) with a
    new object syntax:
    >> instance my_class:
    >> __class__ = object
    >> __metaclass__ = MyMetaClass
    >> ....


    thanks

    Sylvain Ferriol
    Ingénieur de recherche
    Laboratoire TIMC/IMAG
    http://www-timc.imag.fr/
     
    Sylvain Ferriol, Sep 1, 2006
    #1
    1. Advertising

  2. Sylvain Ferriol wrote:
    > hello everybody,
    >
    > i want to talk with you about a question i have in mind and i do not
    > find a answer. it 's simple:
    > why do we not have a beatiful syntax for object definition as we have
    > for class definition ?


    See http://www.python.org/dev/peps/pep-0359 (already rejected by
    Guido).

    Michele Simionato
     
    Michele Simionato, Sep 1, 2006
    #2
    1. Advertising

  3. Sylvain Ferriol wrote:
    > hello everybody,
    >
    > i want to talk with you about a question i have in mind and i do not
    > find a answer. it 's simple:
    > why do we not have a beatiful syntax for object definition as we have
    > for class definition ?


    Python's classes are objects too - instances of their metaclass.

    > we can define a class in python in 2 ways:
    > 1. by using the metaclass constructor
    > my_class = MyMetaClass(....)
    > 2. by using the classic definition syntax:
    > class my_class(object):
    > __metaclass__ = MyMetaClass
    >
    > if i want to instanciate an object, i only have one way to define it:
    > my_obj = my_class(....)
    >
    > why not a better syntax like class syntax ?
    >
    > example:
    >>> instance my_obj:
    >>> __class__ = my_class


    I fail to see how it's "better", nor what would be the use case. But
    anyway I think it could be possible by using the metaclass as class and
    the class as instance. Just make sure the metaclass wraps all methods
    into classmethods and you should be done.

    >
    > with this syntax, we are coherent between objects and classes.
    > why do we have a specific syntax for the class object definition and not
    > for objects of different type?
    >
    > so if i want to define a new class object (my_class for example) with a
    > new object syntax:
    >>> instance my_class:
    >>> __class__ = object
    >>> __metaclass__ = MyMetaClass
    >>> ....

    >
    > thanks
    >
    > Sylvain Ferriol
    > Ingénieur de recherche
    > Laboratoire TIMC/IMAG
    > http://www-timc.imag.fr/
    >



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Sep 1, 2006
    #3
  4. Michele Simionato a écrit :
    > Sylvain Ferriol wrote:
    >
    >>hello everybody,
    >>
    >>i want to talk with you about a question i have in mind and i do not
    >>find a answer. it 's simple:
    >>why do we not have a beatiful syntax for object definition as we have
    >>for class definition ?

    >
    >
    > See http://www.python.org/dev/peps/pep-0359 (already rejected by
    > Guido).
    >

    i do not understand the withdrawal note, what do "different level" mean ?
    do you have an example or is it python core implemantation problem ?
    > Michele Simionato
    >
     
    Sylvain Ferriol, Sep 1, 2006
    #4
  5. Sylvain Ferriol wrote:
    > Michele Simionato a écrit :
    > >
    > > See http://www.python.org/dev/peps/pep-0359 (already rejected by
    > > Guido).
    > >

    > i do not understand the withdrawal note, what do "different level" mean ?
    > do you have an example or is it python core implemantation problem ?


    I asked Guido in person at EuroPython. He said the syntax didn't look
    "right" to him. It is as simple as that.

    Michele Simionato
     
    Michele Simionato, Sep 1, 2006
    #5
  6. Michele Simionato a écrit :
    > Sylvain Ferriol wrote:
    >
    >>Michele Simionato a écrit :
    >>
    >>>See http://www.python.org/dev/peps/pep-0359 (already rejected by
    >>>Guido).
    >>>

    >>
    >>i do not understand the withdrawal note, what do "different level" mean ?
    >>do you have an example or is it python core implemantation problem ?

    >
    >
    > I asked Guido in person at EuroPython. He said the syntax didn't look
    > "right" to him. It is as simple as that.
    >

    note that we can not use the "make syntax" for a function definition
    because the block is not executed until the function call, while the
    block for a class is executed before the class instanciation
    > Michele Simionato
    >
     
    Sylvain Ferriol, Sep 1, 2006
    #6
  7. Sylvain Ferriol wrote:
    > hello everybody,
    >
    > i want to talk with you about a question i have in mind and i do not
    > find a answer. it 's simple:
    > why do we not have a beatiful syntax for object definition as we have
    > for class definition ?
    >
    > we can define a class in python in 2 ways:
    > 1. by using the metaclass constructor
    > my_class = MyMetaClass(....)
    > 2. by using the classic definition syntax:
    > class my_class(object):
    > __metaclass__ = MyMetaClass
    >
    > if i want to instanciate an object, i only have one way to define it:
    > my_obj = my_class(....)
    >
    > why not a better syntax like class syntax ?
    >
    > example:
    >>> instance my_obj:
    >>> __class__ = my_class


    Michele Simionato already pointed you to `PEP 359`_. One of the reasons
    that I withdrew it was that people seemed to feel that you could get
    most of what you want now by defining appropriate metaclasses. In your
    case, for example, the appropriate metaclass and its usage might look like::

    >>> def instance(name, bases, dict):

    ... cls = dict.pop('__class__')
    ... dict.pop('__metaclass__')
    ... dict.pop('__module__') # silently added by class statement
    ... return cls(**dict)
    ...
    >>> class C(object):

    ... def __init__(self, a, b):
    ... self.a = a
    ... self.b = b
    ...
    >>> class c:

    ... __metaclass__ = instance
    ... __class__ = C
    ... a = 'foo'
    ... b = 'bar'
    ...
    >>> c.a, c.b

    ('foo', 'bar')

    Sure, it's misleading to use a class statement when you're not actually
    creating a class, but I guess people felt that wanting to do this was
    uncommon enough that they weren't worried about it.

    ... _PEP 359: http://www.python.org/dev/peps/pep-0359/

    STeVe
     
    Steven Bethard, Sep 1, 2006
    #7
  8. > Michele Simionato already pointed you to `PEP 359`_. One of the reasons
    > that I withdrew it was that people seemed to feel that you could get
    > most of what you want now by defining appropriate metaclasses. In your
    > case, for example, the appropriate metaclass and its usage might look
    > like::
    >
    > >>> def instance(name, bases, dict):

    > ... cls = dict.pop('__class__')
    > ... dict.pop('__metaclass__')
    > ... dict.pop('__module__') # silently added by class statement
    > ... return cls(**dict)
    > ...
    > >>> class C(object):

    > ... def __init__(self, a, b):
    > ... self.a = a
    > ... self.b = b
    > ...
    > >>> class c:

    > ... __metaclass__ = instance
    > ... __class__ = C
    > ... a = 'foo'
    > ... b = 'bar'
    > ...
    > >>> c.a, c.b

    > ('foo', 'bar')
    >
    > Sure, it's misleading to use a class statement when you're not actually
    > creating a class, but I guess people felt that wanting to do this was
    > uncommon enough that they weren't worried about it.
    >

    i know that there is always a solution. But this problem show that
    python and others are not coherent in the syntax (contrary to lisp for
    example).

    with the 'make' syntax, it will be really easy to translate a program or
    a data structure defined in XML format into python syntax.

    i do not know how many use-cases we need for changing a PEP status :)

    another advantage is that we have the same syntax in all definition
    levels: metaclass, class, instance.
    and if we just want to use objects and do a sort of 'prototype
    programming', we can with this syntax.
    example:
    instance my_obj(prototype_obj):
    ...
    ... object specialisation
    ...

    sylvain



    > .. _PEP 359: http://www.python.org/dev/peps/pep-0359/
    >
    > STeVe
     
    Sylvain Ferriol, Sep 4, 2006
    #8
  9. Sylvain Ferriol wrote:
    > with the 'make' syntax, it will be really easy to translate a program or
    > a data structure defined in XML format into python syntax.


    Only if there are no ordering constraints and no need for multiple
    elements with the same name. The make statement was built to mirror the
    the class statement, so the body is just executed in a regular Python
    dict. Hence, you can only have one value for each name, and the order
    in which the names appear is not maintained. There's some discussion of
    workarounds_ for this in the PEP, but they're pretty hackish.

    ... _workarounds:
    http://www.python.org/dev/peps/pep-0359/#customizing-the-dict-in-which-the-block-is-executed

    STeVe
     
    Steven Bethard, Sep 6, 2006
    #9
    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. Jianli Shen
    Replies:
    1
    Views:
    645
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    453
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    473
    Jon Slaughter
    Oct 26, 2005
  4. Pierre Yves
    Replies:
    2
    Views:
    526
    Pierre Yves
    Jan 10, 2008
  5. Steve Litt
    Replies:
    2
    Views:
    199
    Dejan Dimic
    Sep 29, 2008
Loading...

Share This Page