Re: Getting rid of "self."

Discussion in 'Python' started by Nick Coghlan, Jan 7, 2005.

  1. Nick Coghlan

    Nick Coghlan Guest

    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 | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
    Nick Coghlan, Jan 7, 2005
    #1
    1. Advertising

  2. 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).
    Luis M. Gonzalez, Jan 7, 2005
    #2
    1. Advertising

  3. On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <> 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,
    ,
    http://www.brunningonline.net/simon/blog/
    Simon Brunning, Jan 7, 2005
    #3
  4. Nick Coghlan

    Roy Smith Guest

    Simon Brunning <> wrote:
    >On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <> 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.
    Roy Smith, Jan 7, 2005
    #4
  5. Nick Coghlan <> 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...
    Michael Hobbs, Jan 7, 2005
    #5
  6. Nick Coghlan

    John Roth Guest

    "Roy Smith" <> wrote in message
    news:crmdqk$jo6$...
    > Simon Brunning <> wrote:
    >>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <> 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
    John Roth, Jan 7, 2005
    #6
  7. 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
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Jan 7, 2005
    #7
  8. Nick Coghlan

    Roy Smith Guest

    In article <>,
    John Roth <> wrote:
    >
    >"Roy Smith" <> wrote in message
    >news:crmdqk$jo6$...
    >> Simon Brunning <> wrote:
    >>>On 7 Jan 2005 08:10:14 -0800, Luis M. Gonzalez <> 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. :)
    Roy Smith, Jan 7, 2005
    #8
  9. Nick Coghlan

    Sean Ross Guest

    "BJörn Lindqvist" <> wrote in message
    news:...
    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
    Sean Ross, Jan 7, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    578
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    592
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    399
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    372
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    400
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page