473,441 Members | 2,075 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,441 software developers and data experts.

loose methods : Smalltalk asPython

Hi.

One of the things I'd like to do with Python is find a way to
consistently implement Smalltalk's "loose methods". This is a
capability whereby additional methods can be added dynamically to
existing classes.

In some respects, it's possible with Python. While "object" cannot be
touched, it's possible to define

class MutableObject( object ) : pass ;

and derive subclasses from MutableObject instead of object. Thereafter,
for instance, if devising a class

class Foo( MutableObject ) : isFoo = True ; ...

you can also put in its module

MutableObject.isFoo = False ;

So, at least for the inheritance hierarchy descending from
MutableObject, for an arbitrary object instance x,

x.isFoo

will return whether it's a Foo or not, effectively although not
transparently extending the builtin type(.).

Like, then, what of other classes which descend from object and not
MutableObject? You'd love to be able to set methods and attributes
within them. Can you? These provide on-the-spot extensions of their
capability without having to subclass them (not such a big deal) or get
everyone to use the new subclass (a very big deal).

Comments? Suggestions?

Thanks,

-- Jan
Dec 27 '06 #1
3 1044
On Tue, 26 Dec 2006 22:49:30 -0500, Jan Theodore Galkowski wrote:
Hi.

One of the things I'd like to do with Python is find a way to
consistently implement Smalltalk's "loose methods". This is a
capability whereby additional methods can be added dynamically to
existing classes.
You can't modify the built-in classes. I'm not sure that it is a good idea
to allow built-ins to be modified. When I see an int, I like the fact that
I know what the int can do, and I don't have to worry about whether it has
been modified by some piece of code elsewhere.

But custom classes can be modified (the risk of some other code modifying
it is the price you pay for being able to customise the class in the first
place):
>>class MyInt(int):
.... pass
....
>>def odd(self):
.... return bool(self % 2)
....
>>MyInt.odd = odd
n = MyInt(5)
n.odd()
True

In some respects, it's possible with Python. While "object" cannot be
touched, it's possible to define

class MutableObject( object ) : pass ;

and derive subclasses from MutableObject instead of object. Thereafter,
for instance, if devising a class

class Foo( MutableObject ) : isFoo = True ; ...

you can also put in its module

MutableObject.isFoo = False ;

So, at least for the inheritance hierarchy descending from
MutableObject, for an arbitrary object instance x,

x.isFoo

will return whether it's a Foo or not, effectively although not
transparently extending the builtin type(.).
Why not just say isinstance(x, Foo)?

Like, then, what of other classes which descend from object and not
MutableObject? You'd love to be able to set methods and attributes
within them. Can you?
Yes you can. Did you bother to try it?
>>class Shrubbery(object):
.... pass
....
>>Shrubbery.spam = lambda self, n: repr(self) + "spam"*n

Shrubbery().spam(5)
'<__main__.Shrubbery object at 0xb7d06aec>spamspamspamspamspam'

--
Steven.

Dec 27 '06 #2
Jan Theodore Galkowski <jt*********@alum.mit.eduwrote:
Comments? Suggestions?
<http://www.ruby-lang.org>

--
Luc Heinrich
Dec 27 '06 #3
Steven D'Aprano:
You can't modify the built-in classes. I'm not sure that it is a good idea
to allow built-ins to be modified. When I see an int, I like the fact that
I know what the int can do, and I don't have to worry about whether it has
been modified by some piece of code elsewhere.
I think it can be useful, there are many situations where you may need
to change the behavior of built-in dicts, etc, but:
- You need to keep code sorted and tidy to avoid problems. (This is
true for most of Python code too today. For example ObjectPascal gives
you less freedom to shoot yourself in the foot.)
- Allowing the built-in objects to be dynamically modified like that
may slow down the virtual machine some, if you don't design it quite
carefully. Psyco can help here too.

Bye,
bearophile

Dec 27 '06 #4

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

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
7
by: j_mckitrick | last post by:
Hi all! I've heard good things about SmallTalk, and I read that the IDE is extremely productive. Why is this the case? What are the technical points where Python and SmallTalk differ? Is...
1
by: Martin Drautzburg | last post by:
Is there an elegant way for finding unsent methods as in Smalltalk ? I am aware of the fact that due to pythons dynamic typing, no tool in the world can find ALL unsent methods (same in...
16
by: English Teacher | last post by:
Which would be a more useful language to learn, Smalltalk or Pearl? Learning curves any different? Thanks!
8
by: CD | last post by:
I doubt this is the correct group to post this in, but since I couldn't find a better place, here goes. Smalltalk is a very mature, highly portable pure object language. From my view of it, it...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
10
by: Yechezkal Gutfreund | last post by:
I have two subclasses of SpriteModel (1) LocalSprite (2)Sprite Both implement a method called .ToXml() which returns an XmlDocument. But they are different. I instances of these objects...
4
by: Alex Stevens | last post by:
Well, It's 4:10 on Friday Afternoon and I am now in a state of turmoil....... I've just spoken with a trainer (who's opinion would be in far higher regard than mine), and he's just told me that...
14
by: Karsten Dambekalns | last post by:
Hi. Thomas Mlynarczyk wrote: Why do you want them to be private in the first place? I have yet to see code where this really makes sense... And if it does, then the external public API is...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.