member fns w/o args?

Discussion in 'Python' started by Steve Canfield, Jul 9, 2004.

  1. Is fn2() accessible?

    class c:
    def fn1(self):
    print "in fn1()"
    def fn2():
    print "in fn2()"

    Do member functions with no arguments have any meaning?

    -sc
     
    Steve Canfield, Jul 9, 2004
    #1
    1. Advertising

  2. Steve Canfield

    Jeff Epler Guest

    fn2 is pretty useless. You could call it as something like
    c.__dict__['fn2']() if you really wanted to, though.

    Maybe you're trying to find a way to ask about "staticmethod" and
    "classmethod", but didn't know the right terms?

    http://docs.python.org/lib/built-in-funcs.html#l2h-14
    http://docs.python.org/lib/built-in-funcs.html#l2h-63

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)

    iD8DBQFA7teoJd01MZaTXX0RAoTIAJ9HLcDI3qOqzZluGUSxgpHZYjssGgCfV3rG
    yvVNkMYgDEQcb5a64vxivrM=
    =7S/z
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Jul 9, 2004
    #2
    1. Advertising

  3. On 9 Jul 2004, Steve Canfield wrote:

    > Is fn2() accessible?
    >
    > class c:
    > def fn1(self):
    > print "in fn1()"
    > def fn2():
    > print "in fn2()"
    >
    > Do member functions with no arguments have any meaning?


    fn2() is accessible, but will result in an error no matter how you try to
    call it: When functions are defined in this manner, Python marks them as
    methods and expects them to be called with a class instance as the first
    argument, and will complain otherwise. fn2(), however, expects no
    arguments, and will complain if it gets any:

    >>> c.fn2()

    TypeError: unbound method fn2() must be called with c instance as first
    argument (got nothing instead)

    >>> c().fn2() # this is equivalent to c.fn2(c())

    TypeError: fn2() takes no arguments (1 given)

    You can, however, make these methods useable by declaring them as static
    methods:

    class c:
    def fn1(self):
    print "in fn1()"
    def fn2():
    print "in fn2()"

    fn2=staticmethod(fn2)

    staticmethod() converts a method that requires a class instance as its
    first argument into one that doesn't, so now you can do this:

    >>> c.fn2()

    in fn2()

    >>> c().fn2() # now equivalent to c.fn2()

    in fn2()

    Also of interest to you may be classmethod(), which works like
    staticmethod() but instead causes the method to require a class (rather
    than a class instance) as its first argument.
     
    Christopher T King, Jul 9, 2004
    #3
    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. kitcha
    Replies:
    3
    Views:
    2,958
    Charles Bailey
    Nov 24, 2005
  2. Ken Varn
    Replies:
    2
    Views:
    636
    Ken Varn
    Jun 22, 2005
  3. Replies:
    3
    Views:
    497
    David Eppstein
    Sep 17, 2003
  4. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    698
    Pierre Fortin
    Sep 18, 2004
  5. Replies:
    2
    Views:
    270
Loading...

Share This Page