Function Scopes and altering their attributes globally

This is the place for queries that don't fit in any of the other categories.

Function Scopes and altering their attributes globally

Postby BapeNAsians » Thu Jul 31, 2014 3:14 pm

I'm having a small problem with how I alter the attributes or even variables in called functions. To change them globally, I understand you can just declare them as global with the global keyword, but is there any other ways to do this? I don't want to make a lot of variables global.

Kind of like when you pass a variable by its address in c++.
BapeNAsians
 
Posts: 42
Joined: Fri Jan 03, 2014 4:25 am

Re: Function Scopes and altering their attributes globally

Postby Larz60+ » Thu Jul 31, 2014 6:20 pm

Hello,

Please show an example of what is giving you trouble.

Thanks,

Larz
Larz60+
 
Posts: 218
Joined: Thu Apr 03, 2014 4:06 pm

Re: Function Scopes and altering their attributes globally

Postby micseydel » Thu Jul 31, 2014 6:34 pm

If you're familiar with C++, then think of arguments in Python as "references passed by value". That is, you're not receiving a copy of the object so if it's mutable then you can modify what's passed in (though this is generally discouraged) although you cannot reassign the variable that was passed in to a different object. Really, C++ needs to be able to do that because there's no good way to return multiple values. In Python, you can just return a (potentially implicit) tuple, and just as easily "unpack" that tuple. Take this example
Code: Select all
>>> from math import sqrt
>>> def square_and_sqrt(x):
   return x**2, sqrt(x)

>>> bigger, smaller = square_and_sqrt(9)
>>> bigger
81
>>> smaller
3.0

I've noticed this same thing in some Java code at work recently. We created empty lists and passed them into a function to populate them, because Java lacks a nice way to simply return them at the same time from the same function. Python does not have this limitation or require "side effects" of functions to achieve the same result.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1385
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Function Scopes and altering their attributes globally

Postby metulburr » Fri Aug 01, 2014 2:00 am

C++ needs to be able to do that because there's no good way to return multiple values

C++11 added tuples
Code: Select all
#include <iostream>
#include <tuple>
#include <cmath>

std::tuple<int, double>
square_and_sqrt(int x){
    return std::make_tuple(pow(x, 2), sqrt(x));
}

int main(){
    auto t = square_and_sqrt(9);

    std::cout << std::get<0>(t) << "\n"
              << std::get<1>(t)
              << std::endl;
}

Code: Select all
metulburr@ubuntu ~ $ g++ -std=c++11 test.cpp -o test
metulburr@ubuntu ~ $ ./test
81
3
metulburr@ubuntu ~ $


sorry i dont mean to shift away from OP question.

I'm having a small problem with how I alter the attributes or even variables in called functions. To change them globally, I understand you can just declare them as global with the global keyword, but is there any other ways to do this? I don't want to make a lot of variables global.

I dont ever use the keyword global. Instead i use OOP. I may even use too many classes in times there should not be.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], W3C [Linkcheck] and 3 guests

cron