Python Forum
global variables, classes, imports
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global variables, classes, imports
#11
In Python, anytime a variable is referenced, it will be looked for in the local scope, then enclosing scope, up to global. Methods are no different than functions in this way.
Reply
#12
(Feb-19-2017, 04:37 AM)micseydel Wrote: In Python, anytime a variable is referenced, it will be looked for in the local scope, then enclosing scope, up to global. Methods are no different than functions in this way.

but if you want to assign a new value to a variable ...

interesting that self.foo = 1 can create a new attribute in a class instance but setattr() is needed in other cases.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
(Feb-26-2017, 03:28 AM)Skaperen Wrote: interesting that self.foo = 1 can create a new attribute in a class instance but setattr() is needed in other cases.
What do you mean?

self.foo = 1
and
setattr(self, foo, 1)
would yield the same result
Reply
#14
(Feb-26-2017, 09:49 AM)buran Wrote:
(Feb-26-2017, 03:28 AM)Skaperen Wrote: interesting that self.foo = 1 can create a new attribute in a class instance but setattr() is needed in other cases.
What do you mean?

self.foo = 1
and
setattr(self, foo, 1)
would yield the same result

you cannot add a new attribute using the first form, except for methods.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
(Mar-02-2017, 04:29 AM)Skaperen Wrote: you cannot add a new attribute using the first form, except for methods.
What are you going on about?
>>> class Blank:
...     pass
...
>>> a = Blank()
>>> a
<__main__.Blank instance at 0x02B12760>
>>> a.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Blank instance has no attribute 'foo'
>>> a.foo = 5
>>> a.foo
5
>>>
Reply
#16
(Feb-26-2017, 09:49 AM)buran Wrote: setattr(self, foo, 1)

just noticed the stupid accidental mistake. of course it should be setattr(self, 'foo', 1)
Reply
#17
forum_import.py:
EGG = 'eggses'

class Egg(object):

    def __init__(self, style):
        self.style = style

egg = Egg('scrambled')
egg.scrapple = 'bacon'
forum_test.py:
from forum_import import *

HAM = 'hocks'

class Spam(object):

    def __init__(self, spice):
        self.spice = spice

    def test(self):
        print(self.spice)
        if hasattr(self, 'ham'):
            print(self.ham)
        print(HAM)
        print(EGG)
        print(egg.style)
        print(egg.scrapple)

spam = Spam('garlic')
spam.ham = 'Wilbur'
spam.test()
The output of running forum_test.py:
garlic
Wilbur
hocks
eggses
scrambled
bacon
Correct me if I'm wrong, but doesn't this cover everything you're talking about? If you are defining something like HAM, and your Spam instance can't see it, there is some other problem going on. It's not a namespace issue.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best programming practice for imports Curbie 8 1,743 Oct-16-2024, 02:20 AM
Last Post: Curbie
  Sudden Extremely Slow / Failed Python Imports bmccollum 1 1,211 Aug-20-2024, 02:09 PM
Last Post: DeaD_EyE
  Trying to understand global variables 357mag 5 2,284 May-12-2023, 04:16 PM
Last Post: deanhystad
  Organizing several similar classes with overlapping variables 6hearts 7 2,979 May-07-2023, 02:00 PM
Last Post: 6hearts
  Global variables or local accessible caslor 4 2,164 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 2,081 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 1,789 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 3,534 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 2,513 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 2,141 Jun-27-2021, 08:30 PM
Last Post: Hpao

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020