7stud wrote:
On Aug 25, 7:26 pm, Dave Angel <da...@ieee.org> wrote:
Stephen Fairchild wrote:
You are trying to run code in a class that does not exist yet.
def Demo():
    def fact(n):
        if n < 2:
            return 1
        else:
            return n * fact(n - 1)
    return type("Demo", (object,), {"fact": staticmethod(fact), "_classvar":
fact(5)})
Demo =emo()
d =emo()
print d._classvar    # prints 120
print d.fact(7)      # prints 5040
print Demo           # prints <class '__main__.Demo'>

In all these messages, something I haven't seen pointed out is that
fact() has no self argument.

An "argument" is something that is specified in the the function
call.  I assume you are trying to state something like, "fact() is not
defined with a parameter variable named self".  However, that has
never been a requirement in python:


class A(object):

    def fact(n):
        print n

    fact("hello")


a =()
a.fact()

--output:--
hello
<__main__.A object at 0x7faf0>



You're good at nitpicking. I concede the distinction between argument and formal parameter. And self is a convention, not a requirement. But the fact is that the method as written would never have worked, when called from outside the class, since you'd have to call it with either the class name or an instance, and in either case, the method was then trying to do arithmetic on one of those.

Thanks for diluting my point. The OP is chasing the wrong problem. Who cares whether a class initializer can call a method, if the method doesn't meet its original requirements, to be callable outside the class?

And the arguments about how recursion is restricted are ridiculous. Nothing wrong with a method calling itself, once it's got a proper signature. You just have to make the call agree with the signature. The problem is only that the method may not be actually called until the class definition is complete.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to