Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55594 > unrolled thread

Re: multiple values for keyword argument

Started byRoy Smith <roy@panix.com>
First post2011-01-29 09:03 -0500
Last post2011-01-29 23:45 +0000
Articles 9 — 7 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: multiple values for keyword argument Roy Smith <roy@panix.com> - 2011-01-29 09:03 -0500
    Re: multiple values for keyword argument Simon Brunning <simon@brunningonline.net> - 2011-01-31 09:45 +0000
    Re: multiple values for keyword argument Ben Finney <ben+python@benfinney.id.au> - 2011-01-30 08:29 +1100
    Re: multiple values for keyword argument patty@cruzio.com - 2011-01-31 10:00 -0800
    Re: multiple values for keyword argument Ethan Furman <ethan@stoneleaf.us> - 2011-01-29 21:35 -0800
    Re: multiple values for keyword argument Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-02-01 11:27 +0100
    Re: multiple values for keyword argument Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-01-30 09:36 +0000
    Re: multiple values for keyword argument patty@cruzio.com - 2011-01-29 10:39 -0800
    Re: multiple values for keyword argument Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-01-29 23:45 +0000

#55594 — Re: multiple values for keyword argument

FromRoy Smith <roy@panix.com>
Date2011-01-29 09:03 -0500
SubjectRe: multiple values for keyword argument
Message-ID<roy-8F5CD8.09032829012011@news.panix.com>
In article <8qijsgFgu1U1@mid.dfncis.de>,
 Frank Dierkes <Frank.Dierkes@googlemail.com> wrote:

> Naming the first parameter self is only a convention. It could be any 
> other name, too.

But it shouldn't.  The use of "self" is so universal that using anything 
else will just make your code more difficult for other people to 
understand.

[toc] | [next] | [standalone]


#55703

FromSimon Brunning <simon@brunningonline.net>
Date2011-01-31 09:45 +0000
Message-ID<mailman.1492.1296467173.6505.python-list@python.org>
In reply to#55594
On 29 January 2011 18:39,  <patty@cruzio.com> wrote:
>> I, myself, use the spanish word 'yo' instead (less keystrokes, I hate
>> 'self', and it amuses me); if I'm working with my numerical experiments
>> I'll use 'n' or 'x'... although, when posting sample code to c.l.py I do
>> try to use 'self' to avoid possible confusion.  :)
>
> I am glad you said this.  I have been avoiding understanding this 'self',
> just accepting it :}  For the time being, since my programs I am creating
> are for my own use, I think I will make my own names up, that are
> descriptive to me as the programmer, it's all going to be interpreted
> anyway.  And the other email equating to C's argv, etc. - now I get it.

It's perfectly legal to use a name other than self. It's alo perfectly
legal never to wash - and you won't make any friends that way either.

-- 
Cheers,
Simon B.

[toc] | [prev] | [next] | [standalone]


#55711

FromBen Finney <ben+python@benfinney.id.au>
Date2011-01-30 08:29 +1100
Message-ID<877hdn1jqv.fsf@benfinney.id.au>
In reply to#55594
patty@cruzio.com writes:

> > I, myself, use the spanish word 'yo' instead (less keystrokes, I
> > hate 'self', and it amuses me); if I'm working with my numerical
> > experiments I'll use 'n' or 'x'... although, when posting sample
> > code to c.l.py I do try to use 'self' to avoid possible confusion.
> > :)
>
> I am glad you said this. I have been avoiding understanding this
> 'self', just accepting it :} For the time being, since my programs I
> am creating are for my own use, I think I will make my own names up,
> that are descriptive to me as the programmer, it's all going to be
> interpreted anyway.

Please consider that your code is written primarily for humans to read,
and only incidentally for the machine to execute. (If it were primarily
for the machine to execute and communication with humans was not an
issue, you would be writing in machine code.)

Consider that code written “only for my own use” frequently becomes more
widespread; and it's impossible to know at the time of writing it
whether that will be the case. It's prudent to write all such code as
though it were for public consumption.

-- 
 \          “Generally speaking, the errors in religion are dangerous; |
  `\    those in philosophy only ridiculous.” —David Hume, _A Treatise |
_o__)                                           of Human Nature_, 1739 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#55748

Frompatty@cruzio.com
Date2011-01-31 10:00 -0800
Message-ID<mailman.1503.1296496816.6505.python-list@python.org>
In reply to#55594
> On 29 January 2011 18:39,  <patty@cruzio.com> wrote:
>>> I, myself, use the spanish word 'yo' instead (less keystrokes, I hate
>>> 'self', and it amuses me); if I'm working with my numerical experiments
>>> I'll use 'n' or 'x'... although, when posting sample code to c.l.py I
>>> do
>>> try to use 'self' to avoid possible confusion.  :)
>>
>> I am glad you said this.  I have been avoiding understanding this
>> 'self',
>> just accepting it :}  For the time being, since my programs I am
>> creating
>> are for my own use, I think I will make my own names up, that are
>> descriptive to me as the programmer, it's all going to be interpreted
>> anyway.  And the other email equating to C's argv, etc. - now I get it.
>
> It's perfectly legal to use a name other than self. It's alo perfectly
> legal never to wash - and you won't make any friends that way either.
>
> --
> Cheers,
> Simon B.
>
>

Cute.

Believe me, I am learning to change my evil ways.  This is what happens
when you come from a *long* line of self-employed, entrepreneurial people.

Independently Yours,

Patty


[toc] | [prev] | [next] | [standalone]


#55764

FromEthan Furman <ethan@stoneleaf.us>
Date2011-01-29 21:35 -0800
Message-ID<mailman.1465.1296365796.6505.python-list@python.org>
In reply to#55594
patty@cruzio.com wrote:
>> I, myself, use the spanish word 'yo' instead (less keystrokes, I hate
>> 'self', and it amuses me); if I'm working with my numerical experiments
>> I'll use 'n' or 'x'... although, when posting sample code to c.l.py I do
>> try to use 'self' to avoid possible confusion.  :)
> 
> I am glad you said this.  I have been avoiding understanding this 'self',
> just accepting it :}  For the time being, since my programs I am creating
> are for my own use, I think I will make my own names up, that are
> descriptive to me as the programmer, it's all going to be interpreted
> anyway.  And the other email equating to C's argv, etc. - now I get it.

Careful about the names you make-up -- to aid yourself and others you 
don't want to have dozen's of different names that all basically mean 
'this instance that I'm currently working on'.

~Ethan~

[toc] | [prev] | [next] | [standalone]


#55772

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2011-02-01 11:27 +0100
Message-ID<mailman.1535.1296556045.6505.python-list@python.org>
In reply to#55594
Patty wrote:
>
>> patty@cruzio.com wrote:
>>>   I have been avoiding understanding this 'self',
>>> [snip]
>>> Regards,
>>>
>>> Patty
>>>   
>> What is to be understood ?? self references the instance. Did I miss 
>> something ?
>>
>> JM
>>
>>
>>
>
> Yes, there was more.  And it's been fully explained at this point.
>
> Patty


Hmm... I re-read the thread just in case ... Anyway.
I'd like to read suggestions for self replacements...
Currently 'yo' have been proposed.

I'm just curious, I promise I won't make any comment :D

JM

[toc] | [prev] | [next] | [standalone]


#55796

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-01-30 09:36 +0000
Message-ID<4d453127$0$29965$c3e8da3$5496439d@news.astraweb.com>
In reply to#55594
On Sat, 29 Jan 2011 10:39:49 -0800, patty wrote:

> I am glad you said this.  I have been avoiding understanding this
> 'self', just accepting it :}  For the time being, since my programs I am
> creating are for my own use, I think I will make my own names up, that
> are descriptive to me as the programmer, it's all going to be
> interpreted anyway.  And the other email equating to C's argv, etc. -
> now I get it.


That's a shame, because `self` is actually very simple once you 
understand the basic principles of object-oriented programming.

What names would you choose? Unless you're writing descriptors, or using 
class methods, both of which should be considered advanced usage (highly 
advanced for descriptors, moderately so for class methods), it's not like 
every method needs a different descriptive first argument. In English, 
"self", "this", "me" or "instance" would be good names. What else would 
you use?


The idea of method syntax is that you start with an instance of some 
class:

mystring = "hello world"  # an instance of the str class

In procedural languages like C or Pascal, you would call a function and 
give the string as an argument. Python supports this programming model, 
and uses it for built-ins like len:

len(mystring)
=> returns 11


Object oriented programming uses a different syntax. Instead of 

function(instance)

as above, we take the instance argument outside the brackets. For example:

mystring.upper()  # instead of upper(mystring)
=> returns "HELLO WORLD"

If there are any extra arguments needed, they go inside the brackets as 
normal.

So far, this is just a change of syntax. It's like saying "The cat of my 
brother's" vs. "my brother's cat" -- the meaning is the same, but the 
syntax differs. The real advantages of object oriented programming and 
methods come elsewhere (e.g. encapsulation and inheritance).

    [Aside: when I was learning this, the hardest part I found was
    remembering which things were functions, and which were methods. 
    I kept writing (wrongly!) things like:

    "hello world".len()
    upper("hello world")

    Unfortunately there is no easy way to recognise what will be a
    function like len, and which are methods like upper. That will 
    come with experience.

Back to function/procedural syntax versus object oriented syntax... 

One difference, though, is when you write a method definition. Because 
the method you write starts off life as an ordinary function, you need to 
write it *as if* it were a function you call like len() above. Here's how 
you might write a method in a class:

class MyClass:
    def method(self, extra):
        pass

When you then call the method:

instance = MyClass()
instance.method("something extra")

Python automatically changes the syntax for you, and passes two arguments 
to the function as if you did this:

# pseudo-code
set self = instance
set extra = "something extra
extract "method" from MyClass
call method(self, extra)

We call the first argument something like "self" because it will ALWAYS 
be the instance itself. Unlike a regular function, which can have 
anything passed as the first argument, and therefore you should give it a 
descriptive name, the method's first argument is provided automatically 
for you and will always be the instance you started with.



I hope this helps.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#55902

Frompatty@cruzio.com
Date2011-01-29 10:39 -0800
Message-ID<mailman.1462.1296327905.6505.python-list@python.org>
In reply to#55594
> Roy Smith wrote:
>> In article <8qijsgFgu1U1@mid.dfncis.de>,
>>  Frank Dierkes <Frank.Dierkes@googlemail.com> wrote:
>>
>>> Naming the first parameter self is only a convention. It could be any
>>> other name, too.
>>
>> But it shouldn't.  The use of "self" is so universal that using anything
>> else will just make your code more difficult for other people to
>> understand.
>
> Nevertheless, if you have a good reason to, go ahead.
>
> I, myself, use the spanish word 'yo' instead (less keystrokes, I hate
> 'self', and it amuses me); if I'm working with my numerical experiments
> I'll use 'n' or 'x'... although, when posting sample code to c.l.py I do
> try to use 'self' to avoid possible confusion.  :)
>
> ~Ethan~
> --
> http://mail.python.org/mailman/listinfo/python-list

I am glad you said this.  I have been avoiding understanding this 'self',
just accepting it :}  For the time being, since my programs I am creating
are for my own use, I think I will make my own names up, that are
descriptive to me as the programmer, it's all going to be interpreted
anyway.  And the other email equating to C's argv, etc. - now I get it.

Regards,

Patty
>
>

[toc] | [prev] | [next] | [standalone]


#55949

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-01-29 23:45 +0000
Message-ID<4d44a67e$0$29993$c3e8da3$5496439d@news.astraweb.com>
In reply to#55594
On Sat, 29 Jan 2011 09:03:28 -0500, Roy Smith wrote:

> In article <8qijsgFgu1U1@mid.dfncis.de>,
>  Frank Dierkes <Frank.Dierkes@googlemail.com> wrote:
> 
>> Naming the first parameter self is only a convention. It could be any
>> other name, too.
> 
> But it shouldn't.  The use of "self" is so universal that using anything
> else will just make your code more difficult for other people to
> understand.

It's a strong convention, true, but for Python to prohibit names other 
than self would be a serious mistake. It would add complication to the 
parser, for no real benefit. Remember, there's nothing special about 
methods. They're just ordinary functions which happen to be passed an 
extra argument at runtime. Making it compulsory to call the first 
argument "self" would seriously cripple Python's usefulness in at least 
three cases.

(1) Class methods receive the class, not the instance, and the convention 
is to call that first argument "cls". It would be confusing and 
misleading to call it "self".

Similarly, I sometimes use a similar descriptor which combines the 
functionality of class methods and ordinary methods. Since neither "self" 
nor "cls" would be appropriate, I use the name "this" for the first 
argument:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/

Descriptors are a general protocol, so there may be other such 
technologies being used by people.

(2) Sometimes people use functions inside a class namespace as an 
ordinary function, perhaps as a factory function, called at class 
creation time. Here is a toy example that automates the building of 
properties:

class K(object):
    def build(name):  # called at class creation time
        def getter(self):
            return getattr(self, "_" + name)
        def setter(self, value):
            setattr(self, "_" + name, value)
        return property(getter, setter)
    spam = build("spam")
    ham = build("ham")


(3) Because methods are just a wrapper around an ordinary function, you 
can inject almost any function into a class at runtime. You don't know 
what the first argument of that function is called:

>>> class K(object):
...     def __init__(self, value='spam'):
...             self.attr = value
...
>>> def get_attr(obj):
...     return obj.attr
...
>>> k = K()
>>> get_attr(k)
'spam'
>>>
>>> K.method = get_attr
>>> k.method()
'spam'




-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web