Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner class function without self

Peace, everyone! I'm using Python 3.6.3 and I find strange that such construction is possible:

class TestClass(object):
    def __init__(self):
        self.arg = "arg"

    def test():
        print("Hey test")

And using:

>>> TestClass.test()
"Hey test"

I know that in Python there are standard methods with self as parameter (don't know how to call them properly), static methods, class methods, abstract methods.

But what kind of method the test() is? Is it static method?


Edited:

Are there any useful usecases of such method of determining a function inside a class?

like image 833
Gusev Slava Avatar asked Nov 01 '17 11:11

Gusev Slava


People also ask

Does a class function need self?

By using the “self” we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes.

Can we create a function without self in Python?

The short answer is "because you can def thing(args) as a global function, or as a method of another class.

What happens if you don't use self in Python?

If there was no self argument, the same class couldn't hold the information for both these objects. However, since the class is just a blueprint, self allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods.

How do you call a function without self in Python?

Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.


1 Answers

Let me explain with an example:

class TestClass(object):
  def __init__(self):
    self.arg = "arg"

  def test1():
    print("class method test1, Hey test")

  @classmethod
  def test2(cls):
    print("class method test2, Hey test")

  def test3(self):
    print("instance method test3, Hey test")

Look what happens when you call test1 with the class or with the instance:

First:

  TestClass.test1() #called from class
class method test1, Hey test
   TestClass().test1() #created an instance TestClass()
Traceback (most recent call last):
  File "python", line 1, in <module>
TypeError: test1() takes 0 positional arguments but 1 was given

that's because when you create an instance, the self parameter is used, but here, the method has not the self parameter, that's why it brakes.

next one!

   TestClass.test2()
class method test2, Hey test
   TestClass().test2()
class method test2, Hey test

That worked for instance and for class, why? well, as you can see test2(cls) take an argument, cls, here, I'm not using it, so, it's ok that it works.

bring me the next subject, muajaja

  TestClass().test3()
instance method test3, Hey test
   TestClass.test3()
Traceback (most recent call last):
  File "python", line 1, in <module>
TypeError: test3() missing 1 required positional argument: 'self'

That's easy to see, when you call it as class, you haven't provided the self parameter

like image 145
A Monad is a Monoid Avatar answered Nov 10 '22 05:11

A Monad is a Monoid