I've just started learning Python but have hit a little issue when instancing a class. I seem to get errors like:
AttributeError: type object 'Stuff' has no attribute 'a'
or:
AttributeError: class 'Stuff' has no attribute 'a'

Clearly I'm missing something fairly fundamental :/

A quick test program shows what I mean:

class Stuff:
    def __init__(self):
        self.a = 0.02
def create():
    x = Stuff
    y = Stuff
    z = Stuff
    
    a = []
    a.append(x)
    a.append(y)
    a.append(z)
    print a[0].a

I would have thought that the __init__ function would have initialised the elements of Stuff. If I add aline to asign a value to x.a then I get x, y, and z all with this same value. I appear to only creating one instance with my calls to Stuff rather than a new instance each. That or I really have no clue what I'm doing :)

On a side note I'm not really sure when Stuff(object) or Stuff should be in the class definition. But this is not something I've seen explained during my google-fu.

Edited 7 Years Ago by twoshots: n/a

No, wait, I'm an idiot :)

It's x = Stuff(), not x = Stuff of course. Meh.

No, wait, I'm an idiot :)

It's x = Stuff(), not x = Stuff of course. Meh.

You're not an idiot, you're learning! It's good that you've solved your own problem. I actually just read an article about how a programming instructor put a teddy bear on his desk.

When students came in to ask questions about their assignments he'd ask them to ask the question to the teddy bear. The instructor would happily ignore the student and do his own work while the pupil explained their problem to the teddy bear.

Almost 9 times out of 10, the student would have an "Ah ha!" moment and solve their own problem after hearing it out loud. Seems like sometimes the best way to find an answer is to ask ourselves the very question that we are trying to solve!

Anyway, hope you're getting along just fine with Python. Be sure to stop back and post another question in this forum if you hit any snags. We're here to help!

Actually it would be useful if someone could throw me a bone on why python will do
x = Stuff
when really it should be
x = Stuff()
but it doesn't complain, flag a warning etc. What does the former actually do?

doing
x=Stuff
make of x an alias to the Stuff class.
Try this :

class Test:
    def __init__(self):
        self.a="a"

x=Test
print x
y=x()
print y.a

This may be useful if you want to pass an object definition as an argument for a function. Never happened to me yet but it may be interesting in some case...
What "often" happens is to pass a function (without ()) as an argument of a function of another object. This is known as callback. Search the web for more information about callbacks...

>>> class Stuff:
	def __init__(self):
		self.a = 0.02

		
>>> x = Stuff
>>> x
<class __main__.Stuff at 0x012007E0>
>>> # you are not instance the class
>>> x = Stuff()
>>> x
<__main__.Stuff instance at 0x011FF0F8>
>>> # Here you you instance the class
>>> x.a
0.02
>>> y = Stuff()
>>> z = Stuff()
>>> a = []
>>> a.append(x.a)
>>> a.append(y.a)
>>> a
[0.02, 0.02]
>>> # here you only get 0.02 what you maybe whant to do is this
>>> class Stuff:
	def __init__(self, a):
		self.a = a

		
>>> # Now we can pass argument to the instance
>>> x = Stuff(0.02)
>>> y = Stuff(0.5)
>>> z = Stuff(40)
>>> a = []
>>> a.append(x.a)
>>> a.append(y.a)
>>> a.append(z.a)
>>> a
[0.02, 0.5, 40]

>>> b = sum(a)
>>> b
40.520000000000003

>>> print 'the sum is %.2f' % (b)
the sum is 40.52

Edited 7 Years Ago by snippsat: n/a

In case all those '>>>' look messy and confuse you ...

class Stuff:
    def __init__(self):
        self.a = 0.02

x = Stuff
print(x)      # __main__.Stuff
print(Stuff)  # __main__.Stuff

y = Stuff()
print(y)        # <__main__.Stuff instance at 0x009FAE40>
print(Stuff())  # <__main__.Stuff instance at 0x009FAE40>

A little test print is an amazing tool!

Edited 7 Years Ago by vegaseat: n/a

Thanks for the responses.

I have to admit to using print but didn't fully understand what it was telling me. Now I have an idea what's going on it makes sense.

This question has already been answered. Start a new discussion instead.