473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

a new object definition

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/

Sep 1 '06 #1
8 1669
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

Sep 1 '06 #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 ?
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 'o****@xiludom.gro'.split('@')])"
Sep 1 '06 #3
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
Sep 1 '06 #4
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

Sep 1 '06 #5
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
Sep 1 '06 #6
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
Sep 1 '06 #7
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
Sep 4 '06 #8
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-0...ck-is-executed

STeVe
Sep 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
14
by: Rookie | last post by:
Is C an object oriented programming language?
19
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
9
by: Netocrat | last post by:
Any comments on the correctness of the statements 1, 2a, 2b, 3 and 4 in the code below? If they are correct, then the definition of an object as well as that of an lvalue is broken in C99 by the...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
2
by: jordanp | last post by:
Hello, I'm having a little trouble here and I'm hoping that somebody might be able to help me out (win32 console program). First off, I know that I can use class function inside of my struct as...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.