Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Getting rid of "self."

Reply
Thread Tools

Re: Getting rid of "self."

 
 
Nick Coghlan
Guest
Posts: n/a
 
      01-07-2005
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 | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
 
Luis M. Gonzalez
Guest
Posts: n/a
 
      01-07-2005
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).

 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      01-07-2005
On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <(E-Mail Removed)> 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,
(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-07-2005
Simon Brunning <(E-Mail Removed)> wrote:
>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <(E-Mail Removed)> 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.
 
Reply With Quote
 
Michael Hobbs
Guest
Posts: n/a
 
      01-07-2005
Nick Coghlan <(E-Mail Removed)> 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...

 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      01-07-2005

"Roy Smith" <(E-Mail Removed)> wrote in message
news:crmdqk$jo6$(E-Mail Removed)...
> Simon Brunning <(E-Mail Removed)> wrote:
>>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <(E-Mail Removed)> 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

 
Reply With Quote
 
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
Posts: n/a
 
      01-07-2005
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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-07-2005
In article <(E-Mail Removed)>,
John Roth <(E-Mail Removed)> wrote:
>
>"Roy Smith" <(E-Mail Removed)> wrote in message
>news:crmdqk$jo6$(E-Mail Removed)...
>> Simon Brunning <(E-Mail Removed)> wrote:
>>>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <(E-Mail Removed)> 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.
 
Reply With Quote
 
Sean Ross
Guest
Posts: n/a
 
      01-07-2005
"BJörn Lindqvist" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm getting rid of Firefox greg_____@dont.send.email.com Firefox 21 03-09-2006 02:07 AM
Firefox Plugins: Getting Rid Of Them And Other Questions Lone Clapper Firefox 6 01-11-2006 07:18 AM
Getting rid of auto-complete address bar? Adrian - ProjectBlack.com Firefox 2 04-02-2005 10:25 PM
getting RID: "additional plugins are required..." popup top menuNAGbar *nuisance* (firefox 1.0 ) dave Firefox 3 12-05-2004 06:42 PM
Getting rid of viewstate in GET posted forms damonf ASP .Net 2 11-08-2003 07:58 PM



Advertisments