On 22Jan2016 22:14, boB Stepp <robertvst...@gmail.com> wrote:
On Thu, Jan 21, 2016 at 4:57 AM, Steven D'Aprano <st...@pearwood.info> wrote:
Danny is correct. And it is a useful feature too. For instance, we can
add attributes to functions:

def spam(x, y):
    ...

spam.extra_info = "whatever"

A new thing that I did not suspect I could do.  This bothers me for two reasons:

   1)  It does not seem right adding attributes to functions outside
of its definition.

Have you tried to do it from inside? Anyway, a function is just another object. What do you think it should be particularly special?

   2)  spam.extra_info appears to be global:

spam.extra_info = "whatever"
z = 'WOW!'
def print_global_stuff():
       print('z =', z)
       print('spam.extra_info =', spam.extra_info)

No, "spam" is global. So you can name it anywhere in that module; therefore you can name anything inside it. Again, like any other object.

print_global_stuff()
z = WOW!
spam.extra_info = whatever

And I imagine I am being dense about something that is quite obvious
to you:  How is this a useful feature to have?  What does it give me
that is more useful than just saying something like:

just_another_global variable = "whatever"

spam.extra_info = "blah"
snot = spam
print(snot.extra_info)

Consider if you passed "snot" (or "spam") to a function:

 def print_extra_info(obj):
   print(obj.extra_info)

This is not something you could do with just_another_global_variable.

Consider: this is information you want assicated with a specific object. Therefore it really _is_ an arribute of the object so that it can follow it around.

And what bothered me about my original example that started this
thread is that when my typo

humdrum.sigh_strenght = 'high'

was accepted and did not generate an error, it seemed to mean to me
that I was violating my object's data encapsulation.  It just seems to
me that I should not be able to arbitrarily add new attributes from
outside the class definition that created my object.  That seems
similar to having a class Dog, to which from outside the class'
definition, I decide to add a new Dog attribute that all dogs in this
class can now have a tail sticking out of their noses.  I have no
qualms about subclassing Dog with a MutantDog subclass, but adding new
attributes from outside the class definition?  That just does not seem
right to at this point of my understanding.  As in your function
example, I am sure that I am missing something quite obvious, but I am
just not seeing it now.

All you're missing is realising that setting an attribute is not a special operation.

To take an anecdote from elsewhere:

UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. - Doug Gwyn

There's no need to prevent you setting "sigh_strenght". There probably is benefit in linting tools reporting to you about attributes which are set but apparently never used.

Cheers,
Cameron Simpson <c...@zip.com.au>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to