Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object attribute
#1
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=2
why we have to put a func before a, why don't we just write a=2 does it make any difference ?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Also, if i define a name in a function like this:
def func():
    a=2
i can't access a from outside even with func.a syntax , but if i define like this
def func():
    func.a=2
i can access the name with that syntax, why is that ? the latter form has different life span ?
Reply
#4
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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 definition
such 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 variable
a = None
def func():
    global a
    a = 2
Reply
#6
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
Reply
#7
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
Output:
I am ok! test
Reply
#8
scidam Wrote:starting from Python3.7 it is possible to define __getattr__ at module level
I think I already used this with older versions of python.
Reply
#9
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 281 Aug-17-2021, 05:24 PM
Last Post: Franky77
  AttributeError: 'DataFrame' object has no attribute 'Articles' Anldra12 5 1,824 Jun-17-2021, 04:54 PM
Last Post: Larz60+
  AttributeError: 'Node' object has no attribute 'insert' Anldra12 4 1,294 May-11-2021, 10:12 AM
Last Post: Anldra12
  AttributeError: 'tuple' object has no attribute 'format' Anldra12 7 6,230 Apr-13-2021, 07:45 AM
Last Post: Anldra12
Lightbulb 'str' object has no attribute 'to_csv' brunolelli 3 3,526 Mar-25-2021, 06:40 AM
Last Post: ndc85430
  AttributeError: 'Message' object has no attribute 'split' helpme1 2 1,426 Mar-14-2021, 11:25 AM
Last Post: helpme1
  AttributeError class object has no attribute list object scttfnch 5 887 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  AttributeError: 'NoneType' object has no attribute 'next' loves 2 2,783 Dec-15-2020, 11:30 PM
Last Post: bowlofred
  How to Locate an Attribute's Parent Object? calvinsomething 5 933 Nov-13-2020, 01:52 AM
Last Post: calvinsomething
  'NoneType' object has no attribute 'encode' bhagyashree 6 1,961 Nov-05-2020, 03:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020