tags:

views:

101

answers:

4

i need help in writing code for a constructor that takes the parameters x, y and angle. can anyone show me how to write it.

+8  A: 
class MyClass(object):
  def __init__(self, x, y, angle):
    self.x = x
    self.y = y
    self.angle = angle

The constructor is always written as a function called __init__(). It must always take as its first argument a reference to the instance being constructed. This is typically called self. The rest of the arguments are up to the programmer.

The object on the first line is the superclass, i.e. this says that MyClass is a subclass of object. This is normal for Python class definitions.

You access fields (members) of the instance using the self. syntax.

unwind
thanks if possible can someone explain it so i can understand it and learn it
hugh
+5  A: 

See the Python tutorial.

Lukáš Lalinský
thank you appriciate it
hugh
A: 
class MyClass(SuperClass):
    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)
        # do initialization
giolekva
That's not what he asked.
Loïc Wolff
Often a constructor may have extra parameters that should not be passed on to the parent(s) constructor
gnibbler
+1  A: 

Constructors are delcared with __init__(self, rest of params) so in this case:

    def __init__(self, x, y, angle):
     self.x = x
     self.y = y
     self.angle = angle

You can read more here: Class definition in python

Beku