2

Here is my code which is supposed to print self.cake and self.age from the class Settings.

from startup import Settings
class Profile(Settings):
    def __init__(self):
        super(Profile, self).__init__()

    def print_settings(self):
        print self.cake
        print self.age

p = Profile()
p. print_settings()

Other python script

class Settings(object):
    def __init__(self):
        self.cake = 1

    def number(self):
        self.age = 5

But I keep getting:

AttributeError: 'Profile' object has no attribute 'age'

I need to be able to print the variables from the print_settings function.

What should I do?

7
  • add self.cake and self.age to __init__ function Commented Jan 10, 2016 at 19:44
  • no I can't do that, they have to be separate Commented Jan 10, 2016 at 19:45
  • What you mean by "they have to be seperate" ? Commented Jan 10, 2016 at 19:46
  • self.cake needs to be in the init function and self.age needs to be in the number function Commented Jan 10, 2016 at 19:49
  • 1
    define it as self.age=None in __init__ function Commented Jan 10, 2016 at 19:54

1 Answer 1

1

You have to set the age attribute before calling the print_settings method.

One option would be:

p = Profile()
p.number()
p.print_settings()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.