1

I'm familiar with other oop languages but new to Python. i wanted to know if this is the right way to define properties in a class

class TwoInputParameters(object):
def __init__(self,firstParameter,secondParameter,operator):
    self.firstParameter= firstParameter
    self.secondParametr = secondParameter
    self.operator = operator

my second question is - how can i reach this properties when given an object of the TwoInputParameters type as a method's input?

thanks a lot!

1

1 Answer 1

1

Yes, this is the right way to define a class and some attributes in its __init__() method -- in Python we commonly use the term attributes and not properties for names following a dot.

To reference attributes just use the pattern objectName . attributeName:

>>> myObject = TwoInputParameters("hello", "foo", 42) #craete a new instance of the TwoInputParameters class
>>> print(myObject.firstParameter) #reference the firstParameter attibute
   "hello"

>>> myObject.secondParametr = "bar" #change an attribute
>>> print(myObject.firstParameter) 
   "bar"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.