Archived

This topic is now archived and is closed to further replies.

Monty Python

Python self arguments

Recommended Posts

Guest Anonymous Poster   
Guest Anonymous Poster
Because objects were tacked on as an afterthought and the there is no implicit self-referencial variable.

Share this post


Link to post
Share on other sites
Fruny    1658
Because it makes more sense for that parameter to be visible, thereby sparing python programmers from C++ arguments like :

* Why can't I use a pointer-to-member like a pointer ?
* Because C++'s object model is not well-defined and there may or may not be an implicit 'this' pointer passed along in some register and 'ordinary pointers' know nothing about that.

You can call python methods as unbound methods (somewhat like ordinary functions), passing in the first 'self' parameter explicitely yourself :

class Foo(object):
def Bar(self, arg):
print arg

obj = Foo()

obj.Bar(10) # object-style call

Foo.Bar(obj,10) # unbound method call

method = obj.Bar # bound method
method(10) # bound method call

which lets you easily achieve things which are rather painful in C++.

Additionally, functions declared in a class are not necessarily used as instance methods. Static methods and class methods will interpret their first argument (if any) differently.

And anyway, The Zen of Python (type import this in your interpreter) does state "Explicit is better than implicit."

Call that a rationalisation of a design flaw if you want, but I like it better that way.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on July 6, 2003 5:47:30 AM]

Share this post


Link to post
Share on other sites
Flarelocke    410
AP, you must be thinking of Perl.

One reason is so you can do this:

class A:
def __init__(x,y,z):
self.x = x
self.y = y
self.z = z


If it bothers you, you can do things like

class Q:
def blah():
x = self.x
y = self.y
z = self.z


There''s very little overhead from doing that, since the variables are aliases of one another.

Share this post


Link to post
Share on other sites
Arild Fines    968
quote:
Original post by Flarelocke
AP, you must be thinking of Perl.

One reason is so you can do this:

class A:
def __init__(x,y,z):
self.x = x
self.y = y
self.z = z



You can''t do that.
quote:

If it bothers you, you can do things like

class Q:
def blah():
x = self.x
y = self.y
z = self.z


There''s very little overhead from doing that, since the variables are aliases of one another.

You can''t do that either.



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites