4
class TextToNumbers():
    def __init__(self, number):
        self.text = str(number)
        self.chunks = parse_text_to_chunks(self.text)

    def parse_text_to_chunks(text_to_parse):
        #stuff

This is an example of a class I'm building. I want it to define some variables with class methods on initialization. (at least I think that is what I want) I think if I talk about my end goal it is that I have a set of complex information that needs to be available about a class directly at initialization. How do I call a class method at initialization, to define a class variable?

Borromakot
  • 293
  • 1
  • 2
  • 13
  • Why do you want to do that? If you initialize a class variable in `__init__`, what is the point in doing that? – thefourtheye Feb 20 '14 at 02:40
  • Are you looking for `self.parse_text_to_chunks(self.text)`? – BrenBarn Feb 20 '14 at 02:40
  • Not sure. I don't want to have to later tell it to figure it out. Would there be a better way to make that happen? for instance, following the example above, I don't want to have to later say `object.parse_text_to_chunks` I just want it to do it itself automatically and assign that value to a variable. – Borromakot Feb 20 '14 at 02:44
  • as @BrenBarn just pointed out it looks like apparently I CAN do it, I just had some silly wrong syntax issue. – Borromakot Feb 20 '14 at 02:46

1 Answers1

2

If you are looking for a way to call an instance method during initialization, you can use self to call that like this

class TextToNumbers():
    def __init__(self, number):
        self.text = str(number)
        self.chunks = self.parse_text_to_chunks(self.text)
        print self.chunks

    def parse_text_to_chunks(self, text_to_parse):
    # 1st parameter passed is the current INSTANCE on which this method is called
        self.var1 = text_to_parse[1:]
        return self.var1

TextToNumbers(123)

And I believe this is what you really need. But if you want a class method

class TextToNumbers():
    def __init__(self, number):
        self.text = str(number)
        self.chunks = TextToNumbers.parse_text_to_chunks(self.text)
        print self.chunks

    @classmethod
    def parse_text_to_chunks(cls, text_to_parse):
    # 1st parameter passed is the current CLASS on which this method is called
        cls.var1 = text_to_parse[1:]
        return cls.var1

TextToNumbers(123)

But there is no point in creating a class method to initialize a class variable in __init__, since a class variable is shared by all the instances of the class, calling from __init__ will overwrite everytime an object is created.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • So you are saying that because self.chunks is always the same (as it relates to self.text) so I wouldn't want it to be in an __init__? Where would it belong then? – Borromakot Feb 20 '14 at 02:50
  • `__init__` variables are unique to each instance of the class, not the same across each class, so wouldn't I want a method in the class, that allows each instance of the class to figure out its own unique variables? – Borromakot Feb 20 '14 at 02:58
  • @Borromakot You are right about the `__init__` variables, what I am trying to say is, class variables are not the same as instance variables. The variables created in `__init__` with `self.` are called instance variables. – thefourtheye Feb 20 '14 at 03:15
  • okay, I think your example got it all worked out for me, and then my inexperience confused the conversation with incorrect vocabulary. Thanks so much! – Borromakot Feb 20 '14 at 03:36
  • @Borromakot No problem :) Enjoy Python :) – thefourtheye Feb 20 '14 at 03:36
  • @thefourtheye Why are you assigning a temporary variable with `self.var1` or `cls.var1`, won't that mean `var1` continues to exist after `parse_text_to_chunks` has been called alongside `self.chunks`? – raphael Jan 17 '17 at 20:48