Instantiating Classes in python (newbie)

Discussion in 'Python' started by DaBeef, Oct 31, 2005.

  1. DaBeef

    DaBeef Guest

    Hello, I am learning python for work from knowing java,c#,c. I had a
    couple questions.
    1) IntegerClass - to instantiate this class how come I use i =
    IntegerClass.IntegerClass() and then this works while i =
    IntegerClass() i.method. I receive an error.

    2) Also using self in the method (self, dataVal). What does this do
    differently from using (dataval). I am a little confused about the
    bound and unbound methods. What does passing self do different?
    Thank-you for all your help
     
    DaBeef, Oct 31, 2005
    #1
    1. Advertising

  2. DaBeef

    infidel Guest

    > Hello, I am learning python for work from knowing java,c#,c. I had a
    > couple questions.
    > 1) IntegerClass - to instantiate this class how come I use i =
    > IntegerClass.IntegerClass() and then this works while i =
    > IntegerClass() i.method. I receive an error.


    It depends on what IntegerClass is. If you have a module named
    IntegerClass with a class named IntegerClass within it, then you must
    use IntegerClass.IntegerClass to refer to the class, because
    IntegerClass is the module. You could also change the import statement
    to:

    from IntegerClass import IntegerClass

    .... in which case, IntegerClass in your current namespace will be the
    class and not the module.

    This is all an educated guess because you didn't specify what the error
    was.

    > 2) Also using self in the method (self, dataVal). What does this do
    > differently from using (dataval). I am a little confused about the
    > bound and unbound methods. What does passing self do different?
    > Thank-you for all your help


    A bound method is kind of like an instance method in java, it's "bound"
    to a particular instance object. Python automatically inserts a
    reference to the object itself as the first argument to a bound call.
    "self" is just like "this" in Java, but you have to explicitly account
    for it in the method signature.
     
    infidel, Oct 31, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zalek Bloom
    Replies:
    4
    Views:
    8,282
    Tor Iver Wilhelmsen
    Dec 1, 2003
  2. Alex Martelli
    Replies:
    3
    Views:
    301
    Achim Dahlhoff
    Nov 29, 2005
  3. Thufir
    Replies:
    4
    Views:
    116
    7stud --
    Nov 8, 2007
  4. Trevoke
    Replies:
    10
    Views:
    205
    Brian Candler
    Jul 22, 2009
  5. David Karr

    Uses for "self-instantiating classes"?

    David Karr, Sep 1, 2009, in forum: Javascript
    Replies:
    5
    Views:
    150
    David Karr
    Sep 1, 2009
Loading...

Share This Page