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?

4
  • Why do you want to do that? If you initialize a class variable in __init__, what is the point in doing that? Commented Feb 20, 2014 at 2:40
  • Are you looking for self.parse_text_to_chunks(self.text)? Commented Feb 20, 2014 at 2: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. Commented Feb 20, 2014 at 2:44
  • as @BrenBarn just pointed out it looks like apparently I CAN do it, I just had some silly wrong syntax issue. Commented Feb 20, 2014 at 2:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
__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 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.
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 No problem :) Enjoy Python :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.