1

Assume that I have this code (at Python):

a=1
b=2
c=3

def x(a,b,c):
    a=5
    b=6
    c=7

x(a,b,c)
print(a,b,c)

The output will be: 1 2 3

My question is:
There is a why to change the values of a,b,c at x(a,b,c) (without return)?

Thank you!

4
  • 1
    You could make them globals inside x (and not pass them as parameters). There's no way to do this via parameters, as a parameter conveys only a value - there's absolutely no connection to the variable the value came from. Commented Apr 21, 2017 at 20:24
  • The scope of a,b,c is local to the function x. You cannot change the values of a,b,c in the global scope where you print the vaule. Commented Apr 21, 2017 at 20:24
  • 1
    You can use the global keyword. But, why do you want to do that? Use a class. Commented Apr 21, 2017 at 20:25
  • @PCIL the use of global is frowned upon. You should return new values and assign them to new variables if you need them. Commented Apr 21, 2017 at 20:26

4 Answers 4

2

Using the global keyword:

a=1
b=2
c=3

def x():
    global a, b, c
    a = 5
    b = 6
    c = 7

x()
print(a,b,c)
# -> 5 6 7

Using a class:

class Foo(object):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def x(self):
        self.a = 5
        self.b = 6
        self.c = 7


foo = Foo(1, 2, 3)
print(foo.a, foo.b, foo.c)
# -> 1 2 3

foo.x()
print(foo.a, foo.b, foo.c)
# -> 5 6 7
Sign up to request clarification or add additional context in comments.

1 Comment

As i stated, the use of global works, but as a new Python Programmer you should try to avoid it as a default pattern.
0

A workaround would be to convert it into a list, as follows:

a = 1
b = 2
c = 3
l = [a, b, c]

def x(list):
    list[:] = [5, 6, 7]

x(l)

a, b, c = l
print(a, b, c)

Comments

0

As a new Python Programmer, PLEASE AVOID the use of global. Globals may be useful for things like configuration, but for the most part they can cause dangerous side effects if you start to use them as your default programming paradigm. I'd suggest you try a class pattern, or variable assignment pattern.

Class Assignment

class Foo(object):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def update(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

foo = Foo(1,2,3)
foo.update(5,6,7)
print "a: %d, b: %d, c: %d" % (foo.a, foo.b, foo.c)

Variable Assignment

a=1
b=2
c=3

def x():
    a_prime = 5
    b_prime = 6
    c_prime = 7
    return a_prime, b_prime, c_prime
a, b, c = x()
print a, b, c

1 Comment

Don't worry, I used the class and it was very helpful!! Thank you!
0

So you can use the global modifier inside of the function to make the variables refer to the ones outside the function; however, this can result in a "code smell".

So inside the function, place global before those assignments and that should fix your problem.

1 Comment

You can use the edit link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.