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
  Multiple classes in another class Dargonoth 3 89 Sep-08-2017, 12:29 PM
Last Post: Dargonoth
  Creating Classes kalerah 5 93 Sep-06-2017, 09:43 AM
Last Post: kalerah
  Global accessing of parameter to class Mahamutha 3 107 Aug-23-2017, 07:04 PM
Last Post: nilamo
  use of classes in python Lux 2 134 Aug-19-2017, 12:29 PM
Last Post: hbknjr
  python classes prsdll6 14 333 Aug-17-2017, 07:26 PM
Last Post: nilamo
  Troubles with classes, taken from a book sylas 2 228 Jun-05-2017, 08:39 AM
Last Post: sylas
  Question about classes cybercreature 2 379 May-18-2017, 07:27 PM
Last Post: Ofnuts
  Trouble with global variables PickyBiker 8 535 May-17-2017, 04:59 PM
Last Post: buran
  Passing Variables up and down through classes DavidC 2 349 May-09-2017, 01:30 AM
Last Post: DavidC
  Changing Variables within Variables? dave925 9 623 Apr-26-2017, 05:19 AM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)