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
  Troubles with classes, taken from a book sylas 2 132 Jun-05-2017, 08:39 AM
Last Post: sylas
  Question about classes cybercreature 2 187 May-18-2017, 07:27 PM
Last Post: Ofnuts
  Trouble with global variables PickyBiker 8 317 May-17-2017, 04:59 PM
Last Post: buran
  Passing Variables up and down through classes DavidC 2 230 May-09-2017, 01:30 AM
Last Post: DavidC
  Changing Variables within Variables? dave925 9 433 Apr-26-2017, 05:19 AM
Last Post: nilamo
  Instances in classes dannyH 6 331 Apr-20-2017, 05:55 PM
Last Post: dannyH
  Classes and Functions Low_Ki_ 22 847 Apr-01-2017, 09:48 PM
Last Post: Low_Ki_
  NameError with global hsunteik 2 294 Feb-18-2017, 11:27 AM
Last Post: ichabod801
  A question about global variables Goldberg291 3 334 Feb-02-2017, 10:50 PM
Last Post: ichabod801
  Python code with serial port and global undefined marciokoko 13 961 Jan-17-2017, 06:14 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)