Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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.
Quote
#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.
Skaaaaaperen........
Quote
#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
Mekire likes this post
Quote
#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.
Skaaaaaperen........
Quote
#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
>>>
buran likes this post
Quote
#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)
Quote
#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
Buddhist, biker, poet, coder, theist
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Changing Variables within Variables? dave925 9 236 Apr-26-2017, 05:19 AM
Last Post: nilamo
  Instances in classes dannyH 6 162 Apr-20-2017, 05:55 PM
Last Post: dannyH
  Classes and Functions Low_Ki_ 22 451 Apr-01-2017, 09:48 PM
Last Post: Low_Ki_
  NameError with global hsunteik 2 155 Feb-18-2017, 11:27 AM
Last Post: ichabod801
  A question about global variables Goldberg291 3 214 Feb-02-2017, 10:50 PM
Last Post: ichabod801
  Python code with serial port and global undefined marciokoko 13 602 Jan-17-2017, 06:14 PM
Last Post: micseydel
  Global Variables. Migrate code from MatLab Felipe 8 322 Jan-13-2017, 01:19 AM
Last Post: Felipe
  Classes are blowing my mind! mcmxl22 5 410 Jan-03-2017, 05:01 PM
Last Post: snippsat
  Help with classes J125 9 590 Dec-15-2016, 03:38 PM
Last Post: J125
  Classes error BluezamX 2 339 Nov-16-2016, 06:52 PM
Last Post: BluezamX

Forum Jump:


Users browsing this thread: 1 Guest(s)