Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18646 > unrolled thread

Calling a variable inside a function of another class

Started byYigit Turgut <y.turgut@gmail.com>
First post2012-01-07 07:00 -0800
Last post2012-01-10 17:41 -0500
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Calling a variable inside a function of another class Yigit Turgut <y.turgut@gmail.com> - 2012-01-07 07:00 -0800
    Re: Calling a variable inside a function of another class Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-07 16:01 +0000
      Re: Calling a variable inside a function of another class Yigit Turgut <y.turgut@gmail.com> - 2012-01-07 08:18 -0800
        Re: Calling a variable inside a function of another class Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-01-07 14:03 -0500
    Re: Calling a variable inside a function of another class Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-01-10 12:03 +0100
    Re: Calling a variable inside a function of another class Terry Reedy <tjreedy@udel.edu> - 2012-01-10 17:41 -0500

#18646 — Calling a variable inside a function of another class

FromYigit Turgut <y.turgut@gmail.com>
Date2012-01-07 07:00 -0800
SubjectCalling a variable inside a function of another class
Message-ID<5a91e124-b6e9-4e2f-88d6-50a33eb496db@k29g2000vbl.googlegroups.com>
class test(test1):

    def __init__(self, device):
      .
      .
      .
    def _something(self, x=1)
     self.dt = data


if __name__ == "__main__":
     test.something.dt ???

I am trying to call a variable located in a function of a class from
main but couldn't succeed.Any ideas?

[toc] | [next] | [standalone]


#18650

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-01-07 16:01 +0000
Message-ID<4f086c52$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#18646
On Sat, 07 Jan 2012 07:00:57 -0800, Yigit Turgut wrote:

> I am trying to call a variable located in a function of a class from
> main but couldn't succeed.Any ideas?

You cannot access local variables from outside their function. That's why 
they are called *local* variables.

You probably want to access *attributes* of the class or the instance. 
You have to define them first -- you can't access something that doesn't 
exist.

class Test:
    shared = 42  # Shared, class attribute
    def __init__(self):
        self.dt = 23  # Instance attribute, not shared.


print(Test.shared)  # prints 42

However, print(Test.dt) fails because no instance has been created yet, 
and so there is no dt attribute. You have to create an instance first, 
then __init__ will run and create the attribute:

instance = Test()
print(instance.dt)  # prints 23



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#18651

FromYigit Turgut <y.turgut@gmail.com>
Date2012-01-07 08:18 -0800
Message-ID<dfaf55fe-c87a-4aed-83e3-b372bc0ead73@t13g2000vbt.googlegroups.com>
In reply to#18650
On Jan 7, 6:01 pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> On Sat, 07 Jan 2012 07:00:57 -0800, Yigit Turgut wrote:
> > I am trying to call a variable located in a function of a class from
> > main but couldn't succeed.Any ideas?
>
> You cannot access local variables from outside their function. That's why
> they are called *local* variables.
>
> You probably want to access *attributes* of the class or the instance.
> You have to define them first -- you can't access something that doesn't
> exist.
>
> class Test:
>     shared = 42  # Shared, class attribute
>     def __init__(self):
>         self.dt = 23  # Instance attribute, not shared.
>
> print(Test.shared)  # prints 42
>
> However, print(Test.dt) fails because no instance has been created yet,
> and so there is no dt attribute. You have to create an instance first,
> then __init__ will run and create the attribute:
>
> instance = Test()
> print(instance.dt)  # prints 23
>
> --
> Steven

How about assigning the variable as global, wouldn't it be more
effective?

[toc] | [prev] | [next] | [standalone]


#18653

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-01-07 14:03 -0500
Message-ID<mailman.4517.1325963033.27778.python-list@python.org>
In reply to#18651
On Sat, 7 Jan 2012 08:18:10 -0800 (PST), Yigit Turgut
<y.turgut@gmail.com> wrote:

>How about assigning the variable as global, wouldn't it be more
>effective?

	"global somename" only tells the Python interpreter that all
references WITHIN a function definition to "somename" really are
references to "somename" at the MODULE level. 


-=-=-=-=- amodule.py

name1 = 123
name2 = 456
name3 = 789

def afunc(x):
	global name1
	z = x + name1	#okay, it is using the module name1
	name1 = z		#still okay
	y = x + name2	#okay so far, will look up to the module level
	e = y + name3	#due to the next line, this will error
	name3 = e		#no global, name3 is local, and above is undefined
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#18748

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-01-10 12:03 +0100
Message-ID<mailman.4587.1326193399.27778.python-list@python.org>
In reply to#18646
Yigit Turgut wrote:
> class test(test1):
>
>     def __init__(self, device):
>       .
>       .
>       .
>     def _something(self, x=1)
>      self.dt = data
>
>
> if __name__ == "__main__":
>      test.something.dt ???
>
> I am trying to call a variable located in a function of a class from
> main but couldn't succeed.Any ideas?
>   

if __name__ == "__main__":
  aTest = test(whateverdevice)
  print aTest.dt

Some advices:

- a common practice in python is to name classes in CamelCase ( read http://www.python.org/dev/peps/pep-0008/ )
- if dt is a shortcut for data, it's a bad one.
- default values are for loosers, they should be used only to keep backward compatibility (personal opinion, a lot of ppl would disagree)
- "call" is usually reserved for method and function, or any callable object in python. What you're trying to do is to reference an object, not calling it.

JM

[toc] | [prev] | [next] | [standalone]


#18786

FromTerry Reedy <tjreedy@udel.edu>
Date2012-01-10 17:41 -0500
Message-ID<mailman.4616.1326235515.27778.python-list@python.org>
In reply to#18646
> Yigit Turgut wrote:
>> class test(test1):
>>
>> def __init__(self, device):
>> .
>> .
>> .
>> def _something(self, x=1)
>> self.dt = data
>>
>>
>> if __name__ == "__main__":
>> test.something.dt ???
>>
>> I am trying to call a variable located in a function of a class

dt is an attribute of an instance of the class.
   t = test() # create instance
   t._something() # call method that assigns attribute to t
   t.dt  # is not the value of dt for t

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web