2

Imagine we have a class with three methods defined:

class MyClass:

    def instance_method(self):
        return "Instance method called", self

    @classmethod
    def class_method(cls):
        return "Class method called", cls

    @staticmethod
    def static_method():
        return "Static method called"

We instantiate it and call the instance method:

obj1 = MyClass()
obj1.instance_method()
# --> ('Instance method called', <__main__.MyClass at 0x106634588>)

And then we call the class method:

obj1.class_method()
# --> ('Class method called', __main__.MyClass)

I'm having trouble understanding why the instance method is bracketed by '<>', and why it contains a pointer to a location in memory when the class method does not.

cglacet
  • 8,873
  • 4
  • 45
  • 60
Trent Fowler
  • 103
  • 5
  • 5
    Because the default `repr` of an instance is different to the default `repr` of a class. See e.g. https://stackoverflow.com/q/1535327/3001761 – jonrsharpe Mar 19 '19 at 16:52
  • 3
    On python3.7 these two respectively prints: `<__main__.MyClass object at 0x7f1e9468a160>` and ``. – cglacet Mar 19 '19 at 16:56
  • 1
    Ditto for Python 2.7, whether `MyClass` is an old-style or new-style class. – chepner Mar 19 '19 at 16:56
  • @jonrsharpe I called the .__repr__ attribute on the class and the instance and I see what you mean. Would it be accurate to say this is a formatting difference and mostly immaterial? – Trent Fowler Mar 19 '19 at 17:16
  • 1
    You appear to be using IPython, or a Jupyter notebook or something else backed by IPython. This is something you should always mention when asking questions, as IPython does a lot of custom handling that regular Python doesn't. – user2357112 Mar 19 '19 at 17:24
  • 1
    `__repr__` isn't actually being used here. You're seeing IPython's custom printing logic. – user2357112 Mar 19 '19 at 17:25
  • @user2357112 An excellent suggestion, I'll do that moving forward. – Trent Fowler Mar 20 '19 at 19:47

1 Answers1

1

All three serve for different purposes. But let's start with this:

class MyClass:
    def method(x):
        return "method called", x

when you have an instance y = MyClass() and call y.method(), what do you expect for x in the function?

  • If you want x to be the object y, it is the instance method;
  • If you want that to be not y but the class of y, it is the class method (remember you cannot check anything specific to y any more, only the stuff generic for the whole class is allowed); and
  • If you want that to be nothing -- you do not even need to know the object y inside the function, nor the class of y, and hence x should not even exist -- that will be a best fit for static method. We usually use static method for things normally a function can do but try to wrap it under a class so it does not pollute the global name space.
adrtam
  • 6,991
  • 2
  • 12
  • 27
  • This is a good explanation, my question was just about why the instance and class methods had different outputs (answer: their __repr__ attributes are different by default) – Trent Fowler Mar 19 '19 at 17:22