0

How would a class accept varying arguments? For example, if I had a class like this

class Shape(object):
    def __init__(self, sides):
        self.sides = sides

If I call this as Shapes(3), the sides will be 3 AKA a triangle.

How would I do this: If I wanted to just call it as a Shape() with no arguments, it would automatically make the Shape() have self.sides = 4. AKA a square

2 Answers 2

4

Make a default argument:

class Shape(object):
    def __init__(self, sides=4):
        self.sides = sides
Sign up to request clarification or add additional context in comments.

Comments

0

If 'object' is another class then Shape would copy object like this:

class object():
    self.bla = 'bla'
class Shape(object):
    ...

else do this:

class Shape():
    ...

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.