Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Calling a variable inside a function of another class

Reply
Thread Tools

Calling a variable inside a function of another class

 
 
Yigit Turgut
Guest
Posts: n/a
 
      01-07-2012
class test(test1):

def __init__(self, device):
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-07-2012
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
 
Reply With Quote
 
 
 
 
Yigit Turgut
Guest
Posts: n/a
 
      01-07-2012
On Jan 7, 6:01*pm, Steven D'Aprano <steve
(E-Mail Removed)> 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?
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-07-2012
On Sat, 7 Jan 2012 08:18:10 -0800 (PST), Yigit Turgut
<(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      01-10-2012
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

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-10-2012

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining a function inside a function. Whats this feature ? How touse inside a class ? Sur Ruby 4 01-08-2008 02:50 PM
Calling a class' memver function from inside another class philipwinder@googlemail.com C++ 5 08-06-2006 08:42 PM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM



Advertisments