[Tutor] method could be a function warning

Richard Damon Richard at Damon-Family.org
Thu Dec 23 00:19:41 EST 2021


On 12/22/21 11:11 PM, Cameron Simpson wrote:
> On 23Dec2021 15:07, Phil <phillor9 at gmail.com> wrote:
>> Dummy(), not it's real name, doesn't access anything from the gui
>> class. It's used as a helper function, and it's called by several
>> methods that do access a gui widget. So I suppose, based on what you
>> have said, I should us the @staticmethod and leave it at that.
> If it is conceptually for the purposes of the class, then yes.
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>

As a static method, you call call it with self.dummy() or Root.dummy() 
(i.e. either on an instance of the class or the class itself) as by 
makeing it a (static) method, the name is put into the namespace of the 
class.

If you don't want to need to prefix it with anything, you need to make 
it not a member of the class, and then it is just a global.

Unlike some languages, just because you are inside a method of a class, 
Python doesn't automatically assume to start looking in that class for 
names, but all accesses need to be prefixed with the space to look in. 
(This may be your confusion, it isn't like C++ for instance which will 
try to assume the 'this' or 'class' added to the call.)

-- 
Richard Damon



More information about the Tutor mailing list