Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Do classes need "self"?
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Wed Dec 11, 2013 5:43 pm   Post subject: Do classes need "self"?

I was wondering: In python, to make variables within the class you always have to be referring to self. Is there a way to make it like Java that just assumes this so I don['t have to write it every time?
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Thu Dec 12, 2013 12:06 am   Post subject: Re: Do classes need "self"?

You need to use self to make instance variables, omitting it will create class variables.

As far as I know, there is no easy way to make the "self" implicit since that would leave you unable to use class variables, but in principle you could remove class variables (or create a new syntax) and then do something clever (and/or messy) like making all your classes children of some class which will "rewrite" itself with proper syntax at runtime (I think I saw something like this once when I was teaching myself some python and asked myself this very question).
rdrake




PostPosted: Thu Dec 12, 2013 10:11 am   Post subject: Re: Do classes need "self"?

As was stated above, you cannot easily do this (and with good reason).

Consider the following Java class:
Java:
public class Rectangle {
  private int length, width;

  public Rectangle(int length, int width) {
    length = length;
    width = width;
  }

  public static void main(String[] args) {
    Rectangle rec = new Rectangle(2, 1);
    System.out.println(rec.length + " x " + rec.width);
  }
}
What would you expect the output to be? It's "0 x 0", as you hide the instance variable with a local one. I always tell my students off for doing this, as it can lead to unexpected results if you're not careful.

Edit: No answer would be complete without pointing to The Zen of Python. Specifically,

BDFL wrote:
Explicit is better than implicit.
Raknarg




PostPosted: Thu Dec 12, 2013 10:25 am   Post subject: RE:Do classes need "self"?

What do you mean when you say class vs instance variables? Instance are the ones you create through the __init__ method, right?
DemonWasp




PostPosted: Thu Dec 12, 2013 10:37 am   Post subject: RE:Do classes need "self"?

The "class" part of "class variable" means that the variable is attached to the class definition, rather than instances of that class (whereas "instance variable" means "attached to a single instance").

In Java, instance variables are the default. Class variables are marked with "static".
Raknarg




PostPosted: Thu Dec 12, 2013 5:08 pm   Post subject: RE:Do classes need "self"?

So class variables are certain variables that are attached to every instance of that class and are the same across all of the instances(so if you change it anywhere it is now different for all of them), whereas instance variables are only attached to each instance. Right?
Dreadnought




PostPosted: Thu Dec 12, 2013 10:26 pm   Post subject: Re: Do classes need "self"?

That's right, here's an example (this is probably the best way to explain things).

Python:
class A:
    classVar = None
    def __init__(self, val):
        A.classVar = val
        self.instanceVar = val
    def GetVarString(self):
        return '\tclassVar = {0}\n\tinstanceVar = {1}'.format(self.classVar, self.instanceVar)
         
obj1 = A('a')
print('object1:\n' + obj1.GetVarString())

obj2 = A('b')
print('object1:\n' + obj1.GetVarString())
print('object2:\n' + obj2.GetVarString())

Which prints
code:
object1:
        classVar = a
        instanceVar = a
object1:
        classVar = b
        instanceVar = a
object2:
        classVar = b
        instanceVar = b


Edit: I'm using python 3 so you might need to add from future import print_function or something like that to make it work.
Raknarg




PostPosted: Thu Dec 12, 2013 10:35 pm   Post subject: RE:Do classes need "self"?

Oh that's cool. In what siuation would you want to have class variables?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Dec 12, 2013 10:36 pm   Post subject: RE:Do classes need "self"?

Maybe you want to record the number of instances of a class. You can have a class variable that increments in the constructor and decrements in the destructor.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: