Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot change variable defined in "__init__.py" after importing #47179

Closed
crayor mannequin opened this issue May 20, 2008 · 4 comments
Closed

Cannot change variable defined in "__init__.py" after importing #47179

crayor mannequin opened this issue May 20, 2008 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@crayor
Copy link
Mannequin

crayor mannequin commented May 20, 2008

BPO 2930
Nosy @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-05-20.15:47:21.925>
created_at = <Date 2008-05-20.15:16:37.325>
labels = ['interpreter-core', 'type-feature', 'invalid']
title = 'Cannot change variable defined in "__init__.py" after importing'
updated_at = <Date 2008-05-21.07:35:22.815>
user = 'https://bugs.python.org/crayor'

bugs.python.org fields:

activity = <Date 2008-05-21.07:35:22.815>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2008-05-20.15:47:21.925>
closer = 'georg.brandl'
components = ['Interpreter Core']
creation = <Date 2008-05-20.15:16:37.325>
creator = 'crayor'
dependencies = []
files = []
hgrepos = []
issue_num = 2930
keywords = []
message_count = 4.0
messages = ['67128', '67133', '67148', '67149']
nosy_count = 2.0
nosy_names = ['georg.brandl', 'crayor']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue2930'
versions = ['Python 2.3']

@crayor
Copy link
Mannequin Author

crayor mannequin commented May 20, 2008

How can I change a variable that I defined in "__init__.py" of a
package called "common" from inside the .py file that imported the
package? I think there is no way to do this, in contrast to when the
variable is defined any other module of the same package but not in the
specific "__init__.py" module.

Example:
If there is a variable named let's say "fileList" in the
module "var.py" of the package "common" then I could modify the
variable "fileList" from within the importing file like this:

from common.var import *
var.fileList = [ "bla", "blub" ]

So that the change of the value of "fileList" is seen when reading it
inside a function of "var.py" after this modification.

But when the variable is defined inside "__init_.py" then the importing
script cannot access it by writing:

from common import *
fileList = [ "bla", "blub" ]

And it cannot access it by writing:

from common import *
common.fileList = [ "bla", "blub" ]

Why are variables of "__init__.py" module non-modifyable this way and
all others of submodules of the same package are modifyable? Maybe
there is this conceptional problem of the python language here or did I
miss something?

@crayor crayor mannequin added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 20, 2008
@crayor crayor mannequin changed the title Cannot change variable definied in __init_.py after importing Cannot change variable defined in __init_.py after importing May 20, 2008
@crayor crayor mannequin changed the title Cannot change variable defined in __init_.py after importing Cannot change variable defined in "__init__.py" after importing May 20, 2008
@birkenfeld
Copy link
Member

Use

import common
common.fileList = ...

Please ask further question on python-list (comp.lang.python) -- this is
not a bug.

@crayor
Copy link
Mannequin Author

crayor mannequin commented May 21, 2008

Oh yes, I am sorry, I forgot to tell you that I *cannot* change the
import statement as it is part of a framwork by a third-party which I
cannot influence. This is a prerequisite of my task.

Of course you are right that I am not strictly speaking about a bug but
rather about a feature request if no appropriate solution exists.

One solution in my situation could be that I could re-import the
package again using a plain import <package> statement (not a from-
import one as the framework does). Is this the recommended solution or
shouldn't there be a way to modify the variable even if it is imported
by a from-import statement?

Maybe you can give me a reason for your opinion?

@crayor crayor mannequin added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 21, 2008
@birkenfeld
Copy link
Member

As I said, please consult the newsgroup/mailing list for these
questions, it's much better suited for them.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant