user3519870
user3519870

Reputation: 81

Can't access variable from another class python

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?

Upvotes: 2

Views: 2670

Answers (1)

garnertb
garnertb

Reputation: 9584

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

One option would be:

p = Profile()
p.number()
p.print_settings()

Upvotes: 1

Related Questions