473,219 Members | 1,431 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,219 software developers and data experts.

A proposal for attribute lookup failures

Proposal:

When an attribute lookup fails for an object, check the top-level
(and local scope?) for a corresponding function or attribute and apply
it as the called attribute if found, drop through to the exception
otherwise. This is just syntactic sugar.
Example:

a = [1,2,3]

a.len()
# -fails,
# -finds len() in the top-level symbol table,
# -applies len(a)
# -3

a.foobar()
# -fails,
# -no foobar() in scope,
# -raise NameError
Benefits:

- Uniform OO style. Top-levels can be hidden as attributes of data.
Most of the top-level functions / constructors can be considered as
attributes of the data; e.g., an int() representation of a string can
be considered as _part_ of the semantics of the string (i.e., one
_meaning_ of the string is an int representation); but doing it this
way saves from storing the int (etc) data as part of the actual
object. The trade-off is speed for space.

- Ability to "add" attributes to built-in types (which is requested
all the time!!) without having to sub-class a built-in type and
initialize all instances as the sub-class. E.g., one can simply define
flub() in the top-level (local?) namespace, and then use "blah".flub()
as if the built-in str class provided flub().

- Backwards compatible; one can use the top-level functions when
desired. No change to existing code required.

- Seemingly trivial to implement (though I don't know much C). On
attribute lookup failure, simply iterate the symbol table looking for
a match, otherwise raise the exception (like current implementation).
Drawbacks:

- Could hide the fact that an extra (On?) lookup on the symbol table
is necessary for attribute lookup failure. (Maybe there could be a
switch/pragma to enable (or disable) the functionality?)

- As above, attribute lookup failure requires an extra lookup on the
symbol table, when normally it would fall through directly to
exception.

- ???
Disclaimer:

I realize that very often what seems good to me, ends up being half-
assed, backwards and generally bad. So I'd appreciate input on this
proposition. Don't take it that I think the idea is wonderful and am
trying to push it. I am just throwing it out there to see what may
become of it.

Regards,
Jordan
Nov 18 '07 #1
4 1402
On Nov 18, 4:07 am, MonkeeSage <MonkeeS...@gmail.comwrote:
Proposal:

When an attribute lookup fails for an object, check the top-level
(and local scope?) for a corresponding function or attribute and apply
it as the called attribute if found, drop through to the exception
otherwise. This is just syntactic sugar.
[...]
Benefits:
[...]
- Backwards compatible; one can use the top-level functions when
desired. No change to existing code required.
It changes how the following code executes:

--------------------------
def foo(x): return x.foo()
foo(1)
--------------------------

* currently this raises AttributeError
* under your proposal this code would fill the stack and raise a
RuntimeError I guess.
- Seemingly trivial to implement (though I don't know much C). On
attribute lookup failure, simply iterate the symbol table looking for
a match, otherwise raise the exception (like current implementation).
This is not a benefit!
--
Arnaud

Nov 18 '07 #2
On Nov 18, 5:27 am, James Stroud <jstr...@mbi.ucla.eduwrote:
It would be unoriginal of me to suggest that this violates the explicit
is better than implicit maxim. But it does.
That's what I meant about hiding the complexity of an attribute
failure. Though, sometimes implicit is acceptable (e.g., promotion of
int to float by __add__ when RHS is a float). Perhaps not here though.

Also, you have picked the perfect use case for a counter argument:

pyclass NoLen(object):
... pass
...
pylen(NoLen)
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.TypeError'>: object of type 'type' has no len()

So this proposal would send the interpreter through two cycles of trying
to find the proper attribute before it failed.
Ah. Good point. Hadn't thought of that.

--------

On Nov 18, 6:42 am, Arnaud Delobelle <arno...@googlemail.comwrote:
It changes how the following code executes:

--------------------------
def foo(x): return x.foo()
foo(1)
--------------------------

* currently this raises AttributeError
* under your proposal this code would fill the stack and raise a
RuntimeError I guess.
I think it would be easy enough to detect and avoid cyclic references
like that (it's done in other instances in the language). An exception
could be thrown in such a case. But maybe it would be more difficult
than I imagine, leading to even further complexity (bad).

I think that, given that it hides a complex operation (which could
easily lead to abuse by the "unwashed masses"), and that in many cases
multiple lookups in the symbol table would be necessary before
termination (as in James' example above), as well as having to deal
with cases such as you mention, perhaps it's not such a good idea.

Off the cuff, it seemed like a nice feature (and still does, if it
could be implemented without the drawbacks). But such is life. :)

Regards,
Jordan
Nov 18 '07 #3
Ps. Just for kicks, here is a simple ruby 1.8 mock-up of the proposal
(sorry for using ruby, but I don't know enough C to start hacking the
CPython backend; I think that a higher-level example is conceptually
clearer anyhow). Reference cycles are not detected in the example.

#!/usr/bin/ruby

class Object
def method_missing(fun, *args)
begin
TOPLEVEL_BINDING.method(fun).call(self, *args)
rescue
raise(NoMethodError,
%{undefined method `#{fun.to_s}' for "#{self}":String},
[])
end
end
end

def myfun(s1, s2)
s1 + s2
end

puts "foo".myfun("bar")
# -foobar

puts 1.myfun(2)
# -3

puts "foo".nofun("baz")
# -./attr_fail.rb:10: undefined method `nofun' for "foo":String
(NoMethodError)
Nov 18 '07 #4
On 19 Nov., 00:02, MonkeeSage <MonkeeS...@gmail.comwrote:
Ps. Just for kicks, here is a simple ruby 1.8 mock-up of the proposal
(sorry for using ruby, but I don't know enough C to start hacking the
CPython backend; I think that a higher-level example is conceptually
clearer anyhow).
No need to excuse. I think Ruby provides a nice context for discussing
the semantics of top level "open classes". But I think those are
entirely different than your contextual bindings. Note I find your
proposal somewhat confusing since I expect that an attribute is
"owned" by an object and is not an arbitrary contextual property.

Regarding open classes in Python, what about "extension classes"?

class +object:
def len(self):
return self.__len__()

This either adds the len method to the namespace of object or it
creates a new namespace which is associated with the namespace of
object s.t. each object can lookup attributes in this namespace when
default lookup fails. I'm not entirely sure about scope. I think the
lifetime of an extension class shall be determined by the lifetime of
the extended class and not by the scope in which the extension class
is defined. What do you think?

Kay

PS. you can use EasyExtend when you want to provide a language
extension without hacking the CPython runtime. EE was made for such
kinds of experiments.

Nov 19 '07 #5

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

Similar topics

0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
0
by: Carl | last post by:
Hi, I have found a way to map attributes (columns) to column headings. But this runs really slow. Is there a way to improve it? Thanks, Carl <?xml version="1.0" encoding="utf-8" ?>...
13
by: Ian Hickson | last post by:
A group of us have been unofficially working on a proposal of extensions to HTML4's Forms chapter, and would like to get input from a wider range of people now that we think our draft proposal is...
28
by: C# Learner | last post by:
Note ---- Please use a fixed-width font to view this, such as Courier New. Problem
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
3
by: Eric Mahurin | last post by:
Is there a standard way to get a descriptor object for an arbitrary object attribute - independent of whether it uses the descriptor/ property protocol or not. I want some kind of...
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.