0

This should be relatively simple, but I'm just missing something. I am trying to utilize a function from another module which is contained within a class. I can do it easily when there is no class involved.

# a.py
import b
b.name()

--

# b.py
def name():
 print "What is your name?"
class details(object):
 def age():
 print "What is your age?"

When I run a i get the expected result of

What is your name?

However when i try to access "def age()" from another module it keeps giving me trouble.

Some of what I have tried so far...

# c.py
import b
b.details.age()

= TypeError: unbound method age() must be called with details instance as first argument (got nothing instead)

# c.py
from b import details
details.age()

= TypeError: unbound method age() must be called with details instance as first argument (got nothing instead)

# c.py
from b import details
b.details(age)

= NameError: name 'b' is not defined

I have tried a few others as well but too many to reasonably post. What am i doing wrong? What is the syntax to do do this? Is it even possible to execute a function when it is contained within a class in another module?

Thanks in advance

EDIT: Fixed all tabs to spaces as suggested by Mike Graham

asked Apr 29, 2013 at 17:15
2
  • Your original posting was hard to follow because you had mixed spaces and tabs. Use all spaces with no hard tabs (and run python with the -tt option). Commented Apr 29, 2013 at 17:49
  • Thanks, ill try not to make that mistake in future. Commented Apr 30, 2013 at 11:17

1 Answer 1

1

The first parameter of all class methods in Python is a reference to the current object (normally this is called self). However, that said, you seem to be trying to use it as a static method and not as an instance method, so perhaps you meant to use the @staticmethod decorator:

class Details: # class names in Python should generally be CamelCased.
 # please note the comments below
 @staticmethod
 def age():
 print 'What is your age?' 

Or, if you really want it to be an instance method, then you need to add self and change how you're referencing it:

class Details:
 def age(self):
 print 'What is your age?' 
# c.py
from b import Details
#you must create an instance of the class before you can call methods on it.
d = Details() 
d.age()

EDIT

As noted in the comments, it is rare that @staticmethod has a genuine use case (it is often better to organize your code with modules, for example). You will often come across @classmethod as an alternative. Please note, though, that methods decorated with @classmethod have a reference to the current class as the first parameter. This question addresses the major differences.

answered Apr 29, 2013 at 17:20
Sign up to request clarification or add additional context in comments.

6 Comments

Your guess seems to be pretty off-the-wall here. Remember, in the face of ambiguity, refuse the temptation to guess.
@MikeGraham I don't see how it is off the wall, the OP's complaint matches that type of behavior rather directly.
@JoranBeasley Which version of Python is that? Because the example works on 2.7.2 for Windows.
weird you are right ... my apologies :) removed comment (and a +1)
@JoranBeasley, you're probably confusing the useful classmethod with staticmethod. (There is a general lack of usecases in well-designed code for using staticmethod as a decorator.)
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.