Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are class objects/ instances in Python 3 passed by reference?

This program modifies objects of the class "myclass" _x and _y, but I don't pass it as a parameter to the function try_block. How do the objects get modified?

class AddSub:
    def _init_(self): #how do default parameters work? 
        self._x, _y

    def set_x(self, num):
        self._x = num

    def set_y(self, num):
        self._y = num   

    def add(self):
        return self._x + self._y

    def sub(self):
        return self._x - self._y

def try_block():
    try:
        ch = int(input("type 1 to add, and 2 to subtract: "))

        myclass.set_x(int(input("enter an x value: "))) #how does myclass get modifed?

        myclass.set_y(int(input("enter a y value: ")))

        return ch

    except ValueError:
        print("Invalid entry.")

        ch = try_block()

        return ch


myclass = AddSub()

choice = try_block()

if choice == 1:
    print(myclass.add())

elif choice == 2:
    print(myclass.sub())
like image 892
Sal Rosa Avatar asked Nov 21 '12 07:11

Sal Rosa


3 Answers

Before I go into answering your question, I want to mention some things about terminology. Your myclass value is an instance, not a class. You do have a class in your code, named AddSub. When you called AddSub() you created an instance of that class. It's important to learn the right terminology for things like this, so you can ask good questions and understand the answers you get back.

Your code comes close to working because you're saving an instance of the AddSub class to a global variable named myclass. Later, you call some methods on that global variable from the try_block function. This is legal in Python, though generally not recommended.

Instead, you should pass the object as an argument:

def try_block(value):
    try:
        value.set_x(whatever())
    except ValueError:
        pass

You'd call it by passing an instance of your AddSub class to the function:

myAddSub = AddSub() # create the instance
try_block(myAddSub) # pass it to the function

This is much nicer because it doesn't depend on a global variable having a specific value to work and you can call it with many different AddSub instances, if you want.

One part of your code that's currently broken is the AddSub class's constructor. There's no need to declare variables. You can just assign to them whenever you want. If you want to have default values set, you can do that too:

def __init__(self):
    self._x = 0
    self._y = 0

If instead you want to be able to set the values when you construct the object, you can add additional parameters to the __init__ method. They can have default values too, allowing the caller to omit some or all of them:

def __init__(self, x=0, y=0):
    self._x = x
    self._y = y

With that definition, all of these will be valid ways to construct an AddSub instance:

a = AddSub()     # gets default 0 value for both _x and _y
b = AddSub(5)    # gets default 0 value for _y
c = AddSub(y=5)  # gets default 0 value for _x
d = AddSub(2, 3) # no defaults!

Finally a point that is mostly independent of your main question: Your try_block function is both poorly named, and implemented in a more complicated way than necessary. Instead of being recursive, I think it would make more sense as a loop, like in this psuedocode version:

def exception_prone_task():
    while True: # loops forever, or until a "return" or "break" statement happens
         try:
             result = do_stuff_that_might_raise_an_exception()
             return result # you only get here if no exception happened

         except WhateverExceptions as e:
             report_about_about_the_exceptions(e)
like image 54
Blckknght Avatar answered Sep 20 '22 22:09

Blckknght


myclass is defined at the module top level, so it is accessible as a global variable.

like image 33
BrenBarn Avatar answered Sep 22 '22 22:09

BrenBarn


Short answer: everything in Python is passed by reference. (Some people like to add "by reference value").

Longer answer is that your code is a bit confusing. (No problem, it can be improved.)

Firstly, the myclass should not be called that way, because it is not a class. It is an instance of the AddSub class -- i.e. the object of that class.

Secondly, classes should not be given names of the verb. They should be nouns.

The first argument of each class method should be self (for simplicity). The next arguments are the ones that were passed when the class is created (i.e. when the class name is used as if you were calling a function).

Whenever you want to give the argument a default value, you just write =value just after the argument.

Everything inside the method definitions that is part of the future object, must be prefixed by self. (simplified). This way, your __init__ should look like:

    def _init_(self, x=1, y=2): #how do default parameters work? 
        self._x = x
        self._y = y

In my opinion, it is not neccessary to use the _ (underscore) prefix for the object variables.

like image 40
pepr Avatar answered Sep 21 '22 22:09

pepr