This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __init__ taking out of context variables
Type: Stage: resolved
Components: Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, eric.smith, sebasbeco, yselivanov
Priority: normal Keywords:

Created on 2019-11-09 01:07 by sebasbeco, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg356277 - (view) Author: Sebastian Bevc (sebasbeco) Date: 2019-11-09 01:07
Hello,

This is my first bug report. While doing some homework i came to realize that the __init__ of a class was taking out of context variables.

class Foo(object):
  def __init__(self, attr1):
    self.out_of_context = out_of_context


# Raises NameError as it is expected
foo = Foo('some attr')

# 'bar' is bounded to 'out_of_context' although it was initialized
# with value 'some value'
out_of_context = 'bar'
foo = Foo('some value')
print(foo.out_of_context')  # prints 'bar'
msg356279 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-11-09 01:20
This isn't a bug, it's how the language works. You're not forced to use the parameters to a function (in this case __init__), and you can reference any variable, including a global.
msg356280 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-11-09 01:24
Also: you're statement that bar was initialized to "some value" isn't true: you didn't use attr1 in your __init__ method, so "some value" was never used.

If you're confused, I suggest you ask on the python-list or tutor mailing lists. More info here: https://mail.python.org/mailman/listinfo
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82933
2019-11-09 01:24:51eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg356280

stage: resolved
2019-11-09 01:20:20eric.smithsetnosy: + eric.smith
messages: + msg356279
components: - asyncio
2019-11-09 01:07:28sebasbecocreate