473,761 Members | 10,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1686
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('__cla ss__')
... dict.pop('__met aclass__')
... dict.pop('__mod ule__') # 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('__cla ss__')
... dict.pop('__met aclass__')
... dict.pop('__mod ule__') # 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(prototyp e_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
20341
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
14
2937
by: Rookie | last post by:
Is C an object oriented programming language?
19
2914
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 scope declaration int i; This header is included in two files that refer to i but do not declare it. The two files build together into a single program.
100
5280
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
2029
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 following reasoning: foo() does not return an object, so the return of foo() conceptually is not stored, yet we are able to obtain a pointer to one of its member elements, therefore it must be stored, therefore foo() does return an object,...
36
3853
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 program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
2
1838
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 a struct object...but my issue that I'm having is that my class function is set up so that it sets 3 variables... Example of my class object (has the getter(), and setter() functions below this, but I won't display that here):
11
3970
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 is instance of MyEntity class. There is no MyProperty property in MyObject at design time.
275
12365
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
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.