O
O
OccamaRazor2017-03-27 16:08:40
Python
OccamaRazor, 2017-03-27 16:08:40

How to assign multiple classes the same value to a variable internally?

I want to pull out the value of the color of the shape and use it in the future. The point is not to iterate over if pewka.color == "red" or ladja.color =="red"
but to make it a single variable or class:
if figure == "red": #
else: #

class pewka():
    def ___init___(self,x,y,color):
        self.x = x
        self.y = y
        self.color = "red"
class ladja():
    def ___init___(self,x,y,color):
        self.x = x
        self.y = y
        self.color = "red"

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
gill-sama, 2017-03-27
@gill-sama

Try to create a figure ancestor class and write general figure properties into it, such as color, state (if necessary) or any that you think of, then you can refer to the variable to read the figure.color ancestor class property

A
Alexander, 2017-03-27
@fireSparrow

It looks like you don't really understand how classes work.
If you create a self.color attribute, then it is an instance attribute, not a class attribute. You will not be able to access the value of this attribute through the class, this value will be different for each instance.

Q
qlkvg, 2017-03-27
@qlkvg

Everything is very simple
1. Open any python tutorial on the page that tells about OOP in python.
2. Read.
3. Read again carefully.
3.1. Read PEP8
4 as an elective. ?????
5. PROFIT

A
Alexander, 2017-03-28
@sanya84

class Pawn(object):
    def __init__(self,x,y,color='red'):

        self.x = 0
        self.y = 0
        self.color = color
        
  



class Rook(object):
    def __init__(self,x,y,color='red'):

        self.x = 0
        self.y = 0
        self.color = color
        
        
    


if Pawn(0,0) or Rook(0,0):
    pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question