Instantiating Classes in python (newbie)

D

DaBeef

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
 
I

infidel

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top