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
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:
Another unrelated issue, this line
object = pencil()
is hiding the python “object” symbol, most likely not a good idea.