-2

I am trying to truly understand what is going on with class methods in python and came across to the following that I can not understand.

My code:

class exampleclass():

    def __init__(self,text):
        self.text = text

        self.firstletter = text[0]

        # the method has to be run --> with ()
        self.lastletter = self.methodlast()

        # the option that I used to use:
        self.secondletter = self.method2(self.text)

        # with the variable last letter I do something else
        # The order is important. lastletter is already defined here.

    def methodlast(self):
        return self.text[-1]

    def method2(self, text):
        return text[1]

    def methodX5(self):
        # addind a variable to the class
        self.longstring = self.lastletter * 5

My intention is create attributes of the class method by method and I also want the methods to be able to individually be accessed from outside the class.

ideally I would like:

cla_ins = exampleclass("this is a text")

But I also want to use the method independently of a class instance:

second_letter  = exampleclass.method2("whatever")

Using the method calling the class is not OK:

try:
    a =exampleclass.method2("whatever")
    print(a)
except Exception as e: 
    print(str(e))

gives error:

method2() missing 1 required positional argument: 'text'

Using the instance works:

try:
    a = cla_ins.method2("whatever")
    print(a)
except Exception as e: 
    print(str(e))

How should a class be built to have methods to be accessed from inside the class and also from outside?

From inside I mean:

self.secondletter = self.method2(self.text)

From outside I mean:

a = exampleclass.mehtod2("whatever")

It looks like I can only call a method class using an instance of the class. That would make my whole reasoning void. i.e. I use the method with instances or I don't use the method from outside, right?

So far other links consulted:

martineau
  • 119,623
  • 25
  • 170
  • 301
JFerro
  • 3,203
  • 7
  • 35
  • 88

3 Answers3

1

Are you sure you didn't make a typo. mehtod2 != method2

0

It looks like I can only call a method [on a] class using an instance of the class.

That is true.

You can use @classmethod annotation to create a class method though. Note, that a class method cannot change instance state (so method2 in your example is ok to become a class method by adding the annotation and by changing self parameter in the signature to cls for clarity).

Chris
  • 1,335
  • 10
  • 19
  • Sorry guys, typo is corrected. question is partially independent of the typo – JFerro May 02 '20 at 14:48
  • You can accept one answer (if it helps you) by click on big gray check button on its left side. If you wish you can add +10 points to any author of any good answer by click upper gray triangle – Chris May 09 '20 at 11:45
0

I think , you have a typo in your code. You have made the method method2 but calling the method mehtod2 .

Jdeep
  • 1,015
  • 1
  • 11
  • 19