Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:55:07+00:00 2026-06-14T08:55:07+00:00

Long time reader, first time asker. Anyway, Here’s the code I’m working with: class

  • 0

Long time reader, first time asker. Anyway, Here’s the code I’m working with:

class Person(object):
  def __init__(self, s):
    self.name = s
    self.secret = 'I HAVE THE COOKIES'

  @classmethod
  def shout(self):
    print self.name.upper()

class Kid(Person):
  def __init__(self, s):
    super(Kid,self).__init__(s)
    self.age = 12

b = Person('Bob')
k = Kid('Bobby')
print b.name
print k.name
print k.age
print k.secret
k.shout()

Which results in this output and error:

Bob
Bobby
12
I HAVE THE COOKIES
Traceback (most recent call last):
File "a.py", line 22, in <module>
  k.shout()
File "a.py", line 8, in shout
  print self.name.upper()
AttributeError: type object 'Kid' has no attribute 'name'

I assumed that Kid would be able to use the Person’s shout method substituting its (the kid’s) “self” for parent (where the method lives). Apparently, that’s not the case. I know I could declare name outside of init, but that’s both unable to accomodate inputted data and a no-no. Another alternative would be to redefine shout for every child of Person, but that’s a lot of repeated code that I’m trying to avoid.

Thanks very much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:55:08+00:00Added an answer on June 14, 2026 at 8:55 am

    The issue is that @classmethod is a method on a class. It does not have access to an instance’s attributes. Specifically the method is actually passed the class object, thus self is misnamed. You should really call shout‘s argument cls. If you remove the @classmethod then this would all make sense and your code would work as expected.

    As it is, you can think of k.shout() as equivalent to Kid.shout().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

long time reader, first time asker... I am making a music app which uses
Long time reader, first time poster. Working on a web site at http://www.howardpitch.com/ ,
Long time reader, first time poster. Please be gentle :P I'm working on an
Hey! First post for me but long time reader of Stackoverflow. Anyway, got a
long time reader/first time poster here. So I've got a checkbox array that posted
long time reader, first time writer here. I'm trying to figure out all possible
'Long time reader, first time poster' here. I'm in the process of making a
Long term lurker, first time poster here. I have written a class to model
Long time reader/ first time poster here, with a question I suspect will be
Long time reader, first time poster here. I'm creating a test app that creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.