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
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,672 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,865 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,330 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,591 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,900 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,474 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,099 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,352 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,871 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

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