decorat{or,ion}

Mike McClain mike.junk.46 at att.net
Sat May 19 21:07:46 EDT 2018


On Sat, May 19, 2018 at 08:22:59AM +0200, dieter wrote:
> Mike McClain <mike.junk.46 at att.net> writes:
<snip>
>
> An "object", in general, is something that can have attributes
> (holding the object state) and methods (defining often operations on
> the object state but in some cases also general operations (not
> related to the object state).
> In this sense, almost everything you handle in a Python script
> can be view as an object (an exception are the variables;
> they, themselves, are not objects but their values are objects).
>
> In your example, "foo" and "bar" are functions.
> As such, they are also objects - but the feature (attributes,
> methods) is rarely used explicitely (you can use
> "dir(<your function>)" to learn what attributes and methods
> your function has). You use functions in Python typically as
> you are used to use them in another programming language.
>
>
> > ...
> >     For that matter, are baz(), buz() and bug() methods if they only
> > help bar get the job done but don't have a place in bar's interface?
>
> Your functions are methods, if they are defined inside a class.
>
> Typically (there are exceptions), this means that their definition
> occurs at the top level of a "class" statement:
>
>        class CLS...:
>          ...
>          def method(self, ...):
>            ...
>
>        In this case, "method" is a method of class "CLS"
>        and its instances (called "CLS" objects).
>
> When you access a method of an object, then
> the function representing the method is wrapped;
> the wrapper remembers the object and the function and
> ensures that the object is automatically passed as
> the first parameter of the function ("self" in the example above)
> in method calls.
> Thus, the distinction between a method and a "normal" function
> is that for a method, the first parameter is passed automatically
> while in a "normal" function, the call must pass all arguments explicitly.
>
>
    Hi dieter,
        Thanks for the response, this is still a foreign language to me and I need all
    the help I can get. I'm reading the docs, doing the tutorial again but still have
    more questions than answers.
        If I understand what you said, 'taint necessarily so, I'll restate it in psuedo
    code since I've little feel for python syntax yet.

    Let's forget buz().

    def bar(*args):
        (a,b,rest) = parseArgs(args)
        def baz(x):
            ...
        def bug(k,l,m):
            ...
        bug(foo(a), baz(b), rest)

    In 'def bar()', baz & bug are simply functions.
    Are they accessable outside bar?


    class bar(*args):
        (a,b,rest) = parseArgs(args)
        def baz(x):
            ...
        ...

    In 'class bar()' I understand baz and bug should be named _baz_ , _bug_,
    if they're not expected to be called from outside bar but there's nothing
    to prevent one from doing so except manners. Also they're now methods while
    still being functions and had to be declared 'def baz(self,x):'.

    Feel free to laugh if what I'm saying is nonsense in python.

        If I execute 'bar.baz(mu)', assuming mu is enough like b above for
    baz not to throw an exception, can I expect the action of baz to change in a
    manner similar to the change in parameters? Or does something in self prevent that?

    How badly did I miss understand?

    Thanks,
    Mike
--
    I am going to go stand outside so if anyone asks about me,
        tell them I'M OUTSTANDING!



More information about the Python-list mailing list