473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

member fns w/o args?

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
Jul 18 '05 #1
2 1124
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)

iD8DBQFA7teoJd01MZaTXX0RAoTIAJ9HLcDI3qOqzZluGUSxgp HZYjssGgCfV3rG
yvVNkMYgDEQcb5a64vxivrM=
=7S/z
-----END PGP SIGNATURE-----

Jul 18 '05 #2
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.

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Huskier | last post by:
Hi all: I want to pass a class-member function to pthread_create, and my code is in the following, but I don't know how to pass myobj.thread_function to pthread_create function. #include...
4
by: sandeep | last post by:
Hi why we cannot have static as a structure member? & also is there any way to achive data hiding in C at this level( i.e. access only selected structure member ) following code gives syntax...
12
by: Andy Terrel | last post by:
Okay does anyone know how to decorate class member functions? The following code gives me an error: Traceback (most recent call last): File "decorators2.py", line 33, in <module> s.update()...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
3
by: Dan Smithers | last post by:
I want to implement a C++ wrapper to C code that requires a function pointer to be passed in. Specifically, I want a wrapper for pthread that clients can use as a base class. Is there a way of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.