Object attribute - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Object attribute (/thread-19603.html) |
Object attribute - Uchikago - Jul-06-2019 Hi, i'm just a new beginer, so please help me! Today i've come across this syntax module.attribute , i'm wondering what this use for ? And it's syntax looks like when we call method such as list.append(4) ,are both of them related to each other ? And i've seen some functions like this:def func(): func.a=2why we have to put a func before a, why don't we just write a=2 does it make any difference ? RE: Object attribute - ichabod801 - Jul-06-2019 I'm not sure about func.a, but the others are basic use of namespaces. Namespaces are collections of names mapped to Python objects. Two of the common ones are modules and classes (like list). You use the dot operator to access the names in the namespace. There are lots of details in the Python tutorial. The list.append() case is special. This is a bound method of a list class instance. That means it has special features that allow it to operate on the instance it is called from. There is a class tutorial link in my signature below that has details on that and a whole lot of other stuff. RE: Object attribute - Uchikago - Jul-06-2019 Also, if i define a name in a function like this: def func(): a=2i can't access a from outside even with func.a syntax , but if i define like thisdef func(): func.a=2i can access the name with that syntax, why is that ? the latter form has different life span ? RE: Object attribute - ichabod801 - Jul-06-2019 Yes, the second case is an attribute of the function. But that's really a misuse of Python's object structure. If you want to access the value of a function outside of the function, you should return it: def func() a = 2 return a x = func()If for some reason you want that value to stick around as an attribute, func should then be a method in a class, and the class should have the attribute: class Foo(object): def __init__(self): self.a = 0 def meth(self): self.a = 2 foo = Foo() foo.meth() foo.a # 2 RE: Object attribute - Gribouillis - Jul-06-2019 Uchikago Wrote:i can access the name with that syntax, why is that ? the latter form has different life span ?The func.a thing is only a trick to simulate a static variable in a function. After a function definitionsuch as def func(): ...there is an object in the global namespace, named 'func' and it is a function object. You can attach attributes to this object like you would do with most python objects. For example you can write def func(): ... func.foo = 'bar' func.spam = ['eggs', 'ham']This allows you to attach supplementary data to the function, independently from the function's execution. func.a is only one of these data. The author could as well have defined a global variablea = None def func(): global a a = 2 RE: Object attribute - noisefloor - Jul-06-2019 Hi, Quote:Today i've come across this syntax module.attribute, i'm wondering what this use for ?To access attributes of a class, respectively an instance of a class :-) - as mentioned by @ichabod801 , read through the Python tutorial in case you have no full understanding of Python's classes yet. Quote:And it's syntax looks like when we call method such as list.append(4)No, important difference: the round brackets. Big difference! my_module.foo would access the attribute foo of my_module , my_module.foo() would call the method foo() of my_module .Regards, noisefloor RE: Object attribute - scidam - Jul-07-2019 A small note: starting from Python3.7 it is possible to define __getattr__ at module level, e.g.def getattr(name): print("I am ok!", name) import math math.__getattr__ = getattr math.test
RE: Object attribute - Gribouillis - Jul-07-2019 scidam Wrote:starting from Python3.7 it is possible to define __getattr__ at module levelI think I already used this with older versions of python. RE: Object attribute - ichabod801 - Jul-07-2019 (Jul-07-2019, 05:44 AM)Gribouillis Wrote: I think I already used this with older versions of python. It's not working in 3.6 for me. RE: Object attribute - Gribouillis - Jul-07-2019 ichabod801 Wrote:It's not working in 3.6 for me.You're right, it doesn't work directly, but I posted a code snippet for python 2.7 a few years ago that provided basically the same functionality. Who knows? the author of PEP 562 may have read my snippet! I was ahead of my time :) |