3

I've got this class and these functions below. Is there a way to assign the return value inside new_number to the parameter number in initial girl constructor?

class girl(person):
    def __init__(self, number, interest):
        self.number = number
        self.interest = interest
        super().__init__()
    def new_number(self):
        n = '0000000000'
        while '9' in n[3:6] or n[3:6]=='000' or n[6]==n[7]==n[8]==n[9]:
            n = str(random.randint(10**9, 10**10-1))
        return n[:3] + '-' + n[3:6] + '-' + n[6:]
2
  • does new_number function have to be inside Girl class? Commented Nov 23, 2016 at 8:33
  • Er, yes, there is - by calling it. Commented Nov 23, 2016 at 8:34

3 Answers 3

5

If you are always going to assign it to self.number then don't even accept it as a parameter. Simply call it and assign it:

class girl(person):
    def __init__(self, interest):
        self.number = self.new_number()
        self.interest = interest
        super().__init__()

    def new_number(self):
        n = '0000000000'
        while '9' in n[3:6] or n[3:6]=='000' or n[6]==n[7]==n[8]==n[9]:
            n = str(random.randint(10**9, 10**10-1))
        return n[:3] + '-' + n[3:6] + '-' + n[6:]

Note that new_number can be moved outside of the class as it has nothing to do with it, or it can be made static.

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

1 Comment

So dont use it as a paramter, just call it when its needed. Gotcha!
1

Try this it should be working. you need not to pass number since you are going to call new_number method.

class Girl():
    def __init__(self, interest=0):
        self.number = self.new_number()
        self.interest = interest
        super().__init__()
    def new_number(self):
        n = '0000000000'
        while '9' in n[3:6] or n[3:6]=='000' or n[6]==n[7]==n[8]==n[9]:
            n = str(random.randint(10**9, 10**10-1))
        num_from_new_number = n[:3] + '-' + n[3:6] + '-' + n[6:]
        return num_from_new_number

Comments

0

You may just assign the number of self.number:

class girl(person):
    def __init__(self, number, interest):
        self.number = number
        self.interest = interest
        super().__init__()
    def new_number(self):
        n = '0000000000'
        while '9' in n[3:6] or n[3:6]=='000' or n[6]==n[7]==n[8]==n[9]:
            n = str(random.randint(10**9, 10**10-1))
    self.number = n[:3] + '-' + n[3:6] + '-' + n[6:]   # Assign it here

Note that in python, the variable self stands for the object itself, so you can manipulate it directly in your class functions.

You can do assignment or initialization like this self.propertyA = someCalculation()

1 Comment

Wow classes are far more friendly in python than in c++

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.