473,609 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why we will use obj$func() often

"Michael Geary" <Mi**@Geary.com > wrote ...
Does anyone have some sample code where obj$func() would be used?
(Apologies if I missed it.)


There have been so many messages about delegation and binding since Greg
originally posted his meowing cat message that it's hard to remember what
the original problem was that Greg pointed out. At that time Prothon had no
solution for the problem. Now there is a released solution but it is
incomplete and has broken the xor operator ( ^ ) (don't tell anyone <grin>).
I'll restate it here in my own way (this is copied from something I posted
on comp.lang.py).

Statement of problem (These are tested programs from released Python and
Prothon:):

# Python

class klass:
def __init__(self):
self.me = 1
def func(self):
print "func1,self"+st r(self.me),

class klass2(klass):
def __init__(self):
self.me = 2
def func(self):
klass.func(self ) # delegation
print "func2,self"+st r(self.me),

inst = klass2()
inst.func() # prints func1,self2 func2,self2

# Directly translated to Prothon

Klass = Object()
with Klass:
.me = 0 # extra line
def .__init__():
.me = 1
def .func():
print "func1,self"+.m e,

Klass2 = Klass()
with Klass2:
def .__init__():
.me = 2
def .func():
Klass.func() # does not do what python does
print "func2,self"+.m e,

inst = Klass2()
inst.func() # prints func1,self0 func2,self2

As you can see, the call Klass.func() got the function func from the object
Klass and then called it on Klass using Klass as the target "self" instead
of the instance "inst". This is not what Python did and this is the
problem.

In Python the call klass.func() was different because Python knows that
klass is a class and therefore obviously cannot be the target of call (an
instance), so it made the programmer pass the instance self to use as a
parameter.

To fix this, we need to be able to specify in general, what the self object
is going to be during the execution of a function in Prothon (I call this
choice of self "binding self to function" even though it's temporary during
execution).

Greg's solution was to replace the period before the function name in
klass.func() with a different symbol to indicate that the func is not
supposed to be bound to klass as the syntax normally suggests (as in
list.sort!()), but instead defaults to the current self. My final scheme is
almost the same but instead of just defaulting to self, it specifically
forces it to self (this may seem like a semantic difference, but don't say
that to Greg <grin>).

So now we have this solution. Note that delegation to an ancestor prototype
(class in Python), is a common operation, especially in the __init__
function:

Klass = Object()
with Klass:
def $__init__():
$me = 1
def $func():
print "func1,self"+$m e,

Klass2 = Klass()
with Klass2:
def $__init__():
$me = 2
def $func():
Klass$func() # voila! problem fixed
print "func2,self"+$m e,

inst = Klass2()
inst.func() # prints func1,self2 func2,self2

Jul 18 '05 #1
90 3957
My apologies. I posted this to c.l.p. by accident. I meant to post this
to Prothon-users.

"Mark Hahn" <ma**@prothon.o rg> wrote in message
news:d2Uhc.2227 2$dZ1.1600@fed1 read04...
"Michael Geary" <Mi**@Geary.com > wrote ...
Does anyone have some sample code where obj$func() would be used?
(Apologies if I missed it.)
There have been so many messages about delegation and binding since Greg
originally posted his meowing cat message that it's hard to remember what
the original problem was that Greg pointed out. At that time Prothon had

no solution for the problem. Now there is a released solution but it is
incomplete and has broken the xor operator ( ^ ) (don't tell anyone <grin>). I'll restate it here in my own way (this is copied from something I posted
on comp.lang.py).

Statement of problem (These are tested programs from released Python and
Prothon:):

# Python

class klass:
def __init__(self):
self.me = 1
def func(self):
print "func1,self"+st r(self.me),

class klass2(klass):
def __init__(self):
self.me = 2
def func(self):
klass.func(self ) # delegation
print "func2,self"+st r(self.me),

inst = klass2()
inst.func() # prints func1,self2 func2,self2

# Directly translated to Prothon

Klass = Object()
with Klass:
.me = 0 # extra line
def .__init__():
.me = 1
def .func():
print "func1,self"+.m e,

Klass2 = Klass()
with Klass2:
def .__init__():
.me = 2
def .func():
Klass.func() # does not do what python does
print "func2,self"+.m e,

inst = Klass2()
inst.func() # prints func1,self0 func2,self2

As you can see, the call Klass.func() got the function func from the object Klass and then called it on Klass using Klass as the target "self" instead
of the instance "inst". This is not what Python did and this is the
problem.

In Python the call klass.func() was different because Python knows that
klass is a class and therefore obviously cannot be the target of call (an
instance), so it made the programmer pass the instance self to use as a
parameter.

To fix this, we need to be able to specify in general, what the self object is going to be during the execution of a function in Prothon (I call this
choice of self "binding self to function" even though it's temporary during execution).

Greg's solution was to replace the period before the function name in
klass.func() with a different symbol to indicate that the func is not
supposed to be bound to klass as the syntax normally suggests (as in
list.sort!()), but instead defaults to the current self. My final scheme is almost the same but instead of just defaulting to self, it specifically
forces it to self (this may seem like a semantic difference, but don't say
that to Greg <grin>).

So now we have this solution. Note that delegation to an ancestor prototype (class in Python), is a common operation, especially in the __init__
function:

Klass = Object()
with Klass:
def $__init__():
$me = 1
def $func():
print "func1,self"+$m e,

Klass2 = Klass()
with Klass2:
def $__init__():
$me = 2
def $func():
Klass$func() # voila! problem fixed
print "func2,self"+$m e,

inst = Klass2()
inst.func() # prints func1,self2 func2,self2


Jul 18 '05 #2
Mark Hahn wrote:
My apologies. I posted this to c.l.p. by accident. I meant to post this
to Prothon-users.

Oh, no problem, there's some Python content (see below for some comments
on it)...
....
# Python

class klass:
def __init__(self):
self.me = 1
def func(self):
print "func1,self"+st r(self.me),

class klass2(klass):
def __init__(self):
self.me = 2
def func(self):
klass.func(self ) # delegation
print "func2,self"+st r(self.me),

inst = klass2()
inst.func() # prints func1,self2 func2,self2
....
In Python the call klass.func() was different because Python knows that
klass is a class and therefore obviously cannot be the target of call (an
instance), so it made the programmer pass the instance self to use as a
parameter.

This isn't really a very clear description of what's going on in
Python. It won't matter to the Prothon users, but don't want any Python
users to get confused...

Looking up klass.func returns an unbound instance method, this is a
function wrapper which basically just says "hey, you're a member
function of a class, check to be sure that your first argument is a
member of that class". This is done by the function's descriptor
hooks which allow it to return a wrapped object when the user
attempts to retrieve the value from another object (such as a class
or an instance). So, in a sense, yes, the class cannot be the
target of that *particular* call, as the unbound method object you
retrieved will reject anything other than an instance of the class.

Bound instance methods are a similar wrapper, but they say
"curry/bind your first argument (normally self) to this value then
call the underlying function". They are created on object.method
access by the same function descriptor hooks.

The point of all that being that classes most definitely *can* be the
target of a call. Python's classes are first-class objects. In
particular, they are instances of metaclasses, and can have meta-methods
defined which take the class as their first parameter just like a normal
method-call.
class k(type): .... def r( cls ):
.... return 42
.... class x: .... __metaclass__ = k
.... x.r() 42


There's very little "special" about classes other than that they have
some syntactic shortcuts for creating them and for looking up attributes
of their instances within them. Python isn't looking at every method
call and saying "hey, that's a class, that can't be the first parameter
to a function/method!", it (particularly the unbound instance object) is
saying "hey, you're not an instance of my class, go to heck" and never
thinks about whether the object is *particularly* a class or not.

Classes are special in Python, but not nearly as special as you might
think from a class-less perspective :) ,
Mike

By the way, the modern Python idiom is:

super( klass2, self ).func( )

but that wouldn't help in explaining the logic for the Prothon
choice, so no biggie :) .

_______________ _______________ _________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Jul 18 '05 #3
Mike C. Fletcher wrote:
This isn't really a very clear description of what's going on in
Python. It won't matter to the Prothon users, but don't want any
Python users to get confused...
My inability to understand this stuff is what drove me to do Prothon <grin>.
All the hidden wrapped this and wrapped that confused me to no end.
By the way, the modern Python idiom is:

super( klass2, self ).func( )


You're kidding. That seems like a big leap backwards in friendliness. Kind
of a syntax castor oil.

That's explains why Joe Mason did his proposal for Prothon delegation using
that same idiom for Prothon. I thought he was somewhat crazy wanting us to
type all that each time.

What problem caused Python to want to switch to such a general operation?
What is the usage case that is so important that it is making eveyone wear
out their keyboards typing that monstrosity?

Oh well, I guess it gives me one more argument to pitch for Prothon...
Jul 18 '05 #4
Mark Hahn wrote:
My inability to understand this stuff is what drove me to do Prothon
<grin>.
All the hidden wrapped this and wrapped that confused me to no end. ... You're kidding. That seems like a big leap backwards in friendliness.
Kind
of a syntax castor oil.


Says the guy who's introducing a new operator to his language before
even understanding the behavior of the original one, by his own
admission ...

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Human love is often but the encounter of two weaknesses.
-- Francois Mauriac
Jul 18 '05 #5

"Erik Max Francis" <ma*@alcyone.co m> wrote ...
Says the guy who's introducing a new operator to his language before
even understanding the behavior of the original one, by his own
admission ...


Well, I do tend to exaggerate a bit. I can understand it when I study it,
but in everyday coding it does not roll off my fingers. Also, it is
possible to design something new without understanding the old. It might
even be an advantage in some circumstances to not be tainted by old
concepts.

I am serious when I say I think that Python has headed off into egghead land
a bit and I feel that keeps a lot of people from switching to it. I think
that this heady stuff scares them off. I really am trying to make things
simpler in Prothon. Now, whether I can succeed or not is another question.
Only time will tell.
Jul 18 '05 #6
Mark Hahn wrote:
Well, I do tend to exaggerate a bit. I can understand it when I study
it,
but in everyday coding it does not roll off my fingers. Also, it is
possible to design something new without understanding the old.
It is _possible_. But it is not a very good idea.
I am serious when I say I think that Python has headed off into
egghead land
a bit and I feel that keeps a lot of people from switching to it. I
think
that this heady stuff scares them off. I really am trying to make
things
simpler in Prothon. Now, whether I can succeed or not is another
question.
Only time will tell.


Other than the basic premise of Prothon, every single decision I've seen
you make (or consider) looks wholly stylistic, awkward or even arcane,
and the opposite of the one I, or I think Guido, would have chosen.
Being a fan of Io, I think prototype-languages are interesting. Even so
far, I lost interest in looking at the actual main feature of Prothon,
after seeing all the extra baggage that was brought on in unecessary
stylistic changes.

Seriously considering every single possible proposal is not
constructive. Without a strong sense of what the language should look
like, Prothon is going to continue to look more and more like Perl.
It's already most of the way there.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ I love Mickey Mouse more than any woman I've ever known.
-- Walt Disney
Jul 18 '05 #7
Mark Hahn wrote:
I am serious when I say I think that Python has headed off into egghead land
a bit and I feel that keeps a lot of people from switching to it. I think
that this heady stuff scares them off.


Not likely. Most people learning or using Python still have little
interest in or need for metaclass programming, and I *strongly*
doubt it has scared off more than a handful of people.

Likewise, the differences between prototype-based languages and
class-based languages do not appear significant enough to be
the sole reason to pick one over the other. The community,
on the other hand, and the libraries -- well, those are good
reasons for picking Python. It will be interesting to see whether
Prothon manages to achieve similar success in either area.
And if it does, well, see my first point in this sentence again...

-Peter
Jul 18 '05 #8
Mark Hahn wrote:
Mike C. Fletcher wrote:
By the way, the modern Python idiom is:

super( klass2, self ).func( )


You're kidding. That seems like a big leap backwards in friendliness.


Don't worry, the traditional form of super call in Python
isn't going away any time soon. It's not replaced by this;
they do different things.

The new form is occasionally needed, but not very often.
I haven't found a use for it myself yet. (I *thought* I
had found one the other day, but it turned out I hadn't.)

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #9

"Peter Hansen" <pe***@engcorp. com> wrote ...
It will be interesting to see whether
Prothon manages to achieve similar success in either area.


I apologize if I got competitive. As I have said many times, I don't
expect Prothon to compete with Python. It's hard to bust your butt on
something day in and out without getting carried away.
Jul 18 '05 #10

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

Similar topics

34
4749
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a disadvantage of a whole lot of "self." being required everywhere, making the code less readable. Also, since a strength of Python is rapid application development, it slows one down to have to put in all those self.'s. The code seems much cleaner...
5
2116
by: Michael Adkins | last post by:
I have a new ASP project that I need to desperately test on my Windows 2000 machine before posing to my Hosting Company. I am having problems getting the ASP pages to run from LocalHost. I will attempt to give as much information of what I have done to try to correct this without success. First my setup. Windows 2000 Professional with Service Pack 2 installed. IIS is installed. I have turned off the firewall just in case that was a...
6
2748
by: Arturo | last post by:
Hello, I have developed a multiuser application with Microsoft Access 2000 Premium Edition; the application is separate between code and data, the tables are connected, the data is big as 800 MBytes, it works on LAN 100Mbit. Before the application was working on Windows 98SE but now it works on Windows 2000 Professional SP4 and now often the data database become dirty then I have to repair; I am using MDAC 2.8 too. Have somebody some...
10
2106
by: BlueDolphin | last post by:
I'm not sure if this is a question or more of a rant... but I'm looking for some input on this from other developers out there. How often has the following happened to you and how have you dealt with it. I am consulting on a new project. Originally I was going to get data dumped to me from an external system, and import the needed data once a week into a normalized design that I had created. I knew what my fields were going to be,...
5
2412
by: olduncleamos | last post by:
Hi all. Is there any legitimate reason the IP address from a client will change between request? I am wondering if I can autheticate a session by making sure they all come from the same IP. I realize that dial up servers will assign a different IP to the users each time they connect. Any help will be greatly appreciated.
48
4913
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I cannot seem to find many retailers around here that still carry Visual Studio 2003, and some were a...
29
1975
by: walterbyrd | last post by:
Some think it will. Up untill now, Java has never been standard across different versions of Linux and Unix. Some think that is one reason that some developers have avoided Java in favor of Python. Now that Java has been GPL'd that might change. IMO: it won't make much difference. But I don't really know.
0
8109
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
8534
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
8509
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...
1
8188
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
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
6969
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...
1
6034
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4002
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4059
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.