Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Getting rid of "self." (http://www.velocityreviews.com/forums/t340034-re-getting-rid-of-self.html)

Nick Coghlan 01-07-2005 03:20 PM

Re: Getting rid of "self."
 
BJörn Lindqvist wrote:
> So I'm asking here if someone
> knows a better way, maybe using decorators or metaclasses or other
> black magic?


Wait for Python 3k when this will work:

class c:
def __init__(self):
with self:
.x = 1
.y = 2
.hi = "Hi there!"

Cheers,
Nick.

--
Nick Coghlan | ncoghlan@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

Luis M. Gonzalez 01-07-2005 04:10 PM

Re: Getting rid of "self."
 
You can do it easier now without any black magic:

class c:
def __init__(s):
s.x = 1
s.y = 2
s.hi = "Hi there!"

The word "self" is not mandatory. You can type anything you want
instead of self, as long as you supply a keyword in its place (it can
be "self", "s" or whatever you want).


Simon Brunning 01-07-2005 04:21 PM

Re: Getting rid of "self."
 
On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <luismgz@gmail.com> wrote:
> The word "self" is not mandatory. You can type anything you want
> instead of self, as long as you supply a keyword in its place (it can
> be "self", "s" or whatever you want).


You *can*, yes, but please don't, not if there's any chance that
anyone other than you are going to have to look at your code.
'self.whatever' is clearly an instance attribute. 's.whatever' isn't
clearly anything - the reader will have to go off and work out what
the 's' object is.

The self prefix is a perfectly good convention. Let's stick to it.

--
Cheers,
Simon B,
simon@brunningonline.net,
http://www.brunningonline.net/simon/blog/

Roy Smith 01-07-2005 04:36 PM

Re: Getting rid of "self."
 
Simon Brunning <simon@brunningonline.net> wrote:
>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <luismgz@gmail.com> wrote:
>> The word "self" is not mandatory. You can type anything you want
>> instead of self, as long as you supply a keyword in its place (it can
>> be "self", "s" or whatever you want).

>
>You *can*, yes, but please don't, not if there's any chance that
>anyone other than you are going to have to look at your code.
>'self.whatever' is clearly an instance attribute. 's.whatever' isn't
>clearly anything - the reader will have to go off and work out what
>the 's' object is.


+1.

If there is one coding convention which is constant through the Python
world, it's that the first argument to a class method is named
"self". Using anything else, while legal, is just being different for
the sake of being different.

Michael Hobbs 01-07-2005 04:40 PM

Re: Getting rid of "self."
 
Nick Coghlan <ncoghlan@iinet.net.au> wrote:
>
> Wait for Python 3k when this will work:
>
> class c:
> def __init__(self):
> with self:
> .x = 1
> .y = 2
> .hi = "Hi there!"


Python is looking more like JavaScript every day...


John Roth 01-07-2005 04:43 PM

Re: Getting rid of "self."
 

"Roy Smith" <roy@panix.com> wrote in message
news:crmdqk$jo6$1@panix2.panix.com...
> Simon Brunning <simon@brunningonline.net> wrote:
>>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <luismgz@gmail.com> wrote:
>>> The word "self" is not mandatory. You can type anything you want
>>> instead of self, as long as you supply a keyword in its place (it can
>>> be "self", "s" or whatever you want).

>>
>>You *can*, yes, but please don't, not if there's any chance that
>>anyone other than you are going to have to look at your code.
>>'self.whatever' is clearly an instance attribute. 's.whatever' isn't
>>clearly anything - the reader will have to go off and work out what
>>the 's' object is.

>
> +1.
>
> If there is one coding convention which is constant through the Python
> world, it's that the first argument to a class method is named
> "self". Using anything else, while legal, is just being different for
> the sake of being different.


Didn't you mean instance method? Class methods are a different
beast, and the few examples I've seen seem to use the word "klas".

John Roth


=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= 01-07-2005 04:51 PM

Re: Getting rid of "self."
 
Thank you for your replies. But they don't deal with my original
question. :) I have read the thousands of posts all saying "self is
good" and they are right. But this time I want to be different m-kay?
I figure that there might be some way to solve my problem by doing
this:

..def instancevar2locals(method):
.. # Do something magic here so that exec(magic()) is automagically
run each time
.. # the function is invoked.
.. newmethod = method
.. return newmethod

And then in the class definition something like this:

..class A:
.. def __init__(self):
.. self.hi = "hi"
.. def meth(self):
.. print hi
.. meth = instancevar2locals(meth)

But beyond that, I have no idea and I would be grateful if someone
would like to help me with it.
--
mvh Björn

Roy Smith 01-07-2005 05:12 PM

Re: Getting rid of "self."
 
In article <10ttf1slqmefi28@news.supernews.com>,
John Roth <newsgroups@jhrothjr.com> wrote:
>
>"Roy Smith" <roy@panix.com> wrote in message
>news:crmdqk$jo6$1@panix2.panix.com...
>> Simon Brunning <simon@brunningonline.net> wrote:
>>>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <luismgz@gmail.com> wrote:
>>>> The word "self" is not mandatory. You can type anything you want
>>>> instead of self, as long as you supply a keyword in its place (it can
>>>> be "self", "s" or whatever you want).
>>>
>>>You *can*, yes, but please don't, not if there's any chance that
>>>anyone other than you are going to have to look at your code.
>>>'self.whatever' is clearly an instance attribute. 's.whatever' isn't
>>>clearly anything - the reader will have to go off and work out what
>>>the 's' object is.

>>
>> +1.
>>
>> If there is one coding convention which is constant through the Python
>> world, it's that the first argument to a class method is named
>> "self". Using anything else, while legal, is just being different for
>> the sake of being different.

>
>Didn't you mean instance method? Class methods are a different
>beast, and the few examples I've seen seem to use the word "klas".


Sorry, yes. My bad.

I used to work with a C++ guy who always used "class" when he should
have used "instance". It drove me crazy. :-)

Sean Ross 01-07-2005 10:09 PM

Re: Getting rid of "self."
 
"BJörn Lindqvist" <bjourne@gmail.com> wrote in message
news:mailman.306.1105116676.22381.python-list@python.org...
Thank you for your replies. But they don't deal with my original
question. :) I have read the thousands of posts all saying "self is
good" and they are right. But this time I want to be different m-kay?
I figure that there might be some way to solve my problem by doing
this:
[snip ...]
But beyond that, I have no idea and I would be grateful if someone
would like to help me with it.


http://starship.python.net/crew/mwh/hacks/selfless.py




All times are GMT. The time now is 07:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.