Reputation: 37
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
Upvotes: 0
Views: 50
Reputation: 945
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():
...
Upvotes: 0
Reputation: 249153
Make a default argument:
class Shape(object):
def __init__(self, sides=4):
self.sides = sides
Upvotes: 4