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

  • SEARCH
  • Home
  • 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 4078866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:43:40+00:00 2026-05-20T17:43:40+00:00

this is a best practices type of question. I want to access the attributes

  • 0

this is a best practices type of question. I want to access the attributes and methods of one class from another(maybe this is a bad practice in itself) and what i’m doing to achieve this is

class table():
    def __init__(self):
        self.color = 'green'
    colora = 'red' 

    def showColor(self):
        print('The variable inside __init__ is called color with a value of '+self.color)
        print('The variable outside all methos called colora has a value of '+colora)

class pencil(table):
    def __init__(self):
        print('i can access the variable colora from the class wtf its value is '+table.colora)
        print('But i cant access the variable self.color inside __init__ using table.color ')

object = pencil()
>>>
i can access the variable colora from the class wtf its value is red
But i can't access the variable self.color inside __init__ using table.color 
>>>

As you can see i’m making an instance of the class pencil and as its defined in the class i’m using the notation to inherit from one class to another.

I’ve read everywhere people declaring their classes attributes inside init does that mean that i shouldnt be accessing other classes without using instances of it?
I think this is a problem of inheritance but i can’t get the concept into my head, and i’ve read a few explanations in books and tutorials.

In the end i just want to be able to access the attributes and methods of one class with another.
Thanks

  • 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-05-20T17:43:41+00:00Added an answer on May 20, 2026 at 5:43 pm

    more explixitly, if you want to access table.color from pencil, you don’t need to wrap it in a method, but you need to call the table constructor first:

    class table():
    
        colora = 'red'
        def __init__(self):
            self.color = 'green'
            print 'table: table.color', self.color
            print 'table: table.color', table.colora
    
    class pencil(table):
    
        def __init__(self):
            table.__init__(self)
            print 'pencil: table.colora', table.colora
            print 'pencil: pencil.color',  self.color
            print 'pencil: pencil.colora', pencil.colora
    
    obj = pencil()
    

    Another unrelated issue, this line

    object = pencil()

    is hiding the python “object” symbol, most likely not a good idea.

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

Sidebar

Related Questions

This is a best practices question Let say, I have a class object, like
Colleagues, Preamble. My question is more about best practices. I know one workaround. This
This is a best practices question. I am making an array type * x
Yesterday, I asked this question regarding best practices for a simple information retrieval system
This is really just a best practices question... I find that When developing an
This is basically a 'best practices' question. Struts 1 forms have getters and setters
I think this is just a Best Practices question, but I was wondering if
This may be a question about convention, best practices and/or personal preferences: So I'm
I know this is a subjective question, but I'm always curious about best-practices in
This is more of an architecture/best practices question than anything else, so please feel

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.