1

Hi I'm a newbie in python programming. Please help me with this problem in python3:

pack.py

class one:

    def test(self):
        number = 100   ######I want to access this value and how?
        print('test')

class two:

    def sample(self):
        print('sample')

another.py

from pack import *

class three:

    def four(self):
        obj = one()
        print(obj.test())

###### I want to access the number value in this file and i don't know how #######

obj = three()
obj.four()

7
  • 2
    These are not classes but functions. Commented Feb 4, 2020 at 12:48
  • First, you might be confusing classes and functions. Secondly, I guess you are asking about imports, but I am not sure. Can you clarify? Commented Feb 4, 2020 at 12:48
  • I ask both @tst both are implement in one Commented Feb 4, 2020 at 12:54
  • 1
    number is a function's local variable. It's not part of any class, and doesn't even exist unless the function is being executed. What are you trying to achieve here? Commented Feb 4, 2020 at 13:07
  • @MisterMiyagi am not come to achieve anything i have doubt so i asked question. Thx for answering my question :) Commented Feb 4, 2020 at 13:15

2 Answers 2

3

Here is an alternative pack.py

class One:
    def __init__(self):
        self.number = 100

    def test(self):
        print('test')

class Two:
    def sample(self):
        print('Sample')

another.py

from pack import *

class Three:
    def four(self):
        self.obj = One().number
        return self.obj

three = Three().four()
print(three)

By what seems to be your approach, you were using classes to access variables. It is better to instantiate variables in a constructor ( init method in class One). Then import the class and access it in another class of another file.

Also, it is a good practice to name classes beginning with uppercase letters. There are more possible ways but hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

number needs to be in a global scope, that means outside of a function definition (it shouldn't be indented)

if the variable is inside a function it is impossible to get it in another file

pack.py

number = 100
def test():
   test.other_number = 999 # here we assigne a variable to the function object.
   print("test")

another.py

import pack

pack.test()
print(pack.number)

print(test.other_number) # this only works if the function has been called once

Alternatively if you are using classes:

pack.py

class Someclass():
    other_number = 999 # here we define a class variable

    def __init__(self):
        self.number = 100 # here we set the number to be saved in the class

    def test(self):
        print(self.number) # here we print the number

another.py

import pack

somclass_instance = pack.Someclass() # we make a new instance of the class. this runs the code in __init__
somclass_instance.test() # here we call the test method of Someclass
print(somclass_instance.number) # and here we get the number

print(Someclass.other_number) # here we retrieve the class variable

12 Comments

yeah i know this method but otherwise any other method to call the value like same
@vraj i added in how to use a class variable for this, class variables are accessible from Class and instance of that class. ive also added a example for adding variables to the function object
I dont understand why we are allowed to assign an attribute to a function object?
@EkremDİNÇEL: Because why not? It's unusual, but functions need attributes already (e.g. their docstrings), so they were allowed to support arbitrary attributes.
Well, complex numbers need attributes too, but we cant assing a new attribute to them. I wonder is there any document about that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.