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

Deepend
  • 4,057
  • 17
  • 60
  • 101

1 Answers1

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.

Community
  • 1
  • 1
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 3
    Your guess seems to be pretty off-the-wall here. Remember, *in the face of ambiguity, refuse the temptation to guess*. – Mike Graham Apr 29 '13 at 17:25
  • @MikeGraham I don't see how it is off the wall, the OP's complaint matches that type of behavior rather directly. – cwallenpoole Apr 29 '13 at 17:30
  • @JoranBeasley Which version of Python is that? Because the example works on 2.7.2 for Windows. – cwallenpoole Apr 29 '13 at 17:32
  • weird you are right ... my apologies :) removed comment (and a +1) – Joran Beasley Apr 29 '13 at 17:34
  • 3
    @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.) – Mike Graham Apr 29 '13 at 17:34
  • Thanks for this, i did not know that i had to create an instance of the class to call it. – Deepend Apr 29 '13 at 17:44