Home › Hindi › AttributeError Hindi में

Python AttributeError Hindi में — "object has no attribute" का मतलब

🌐 यह page Hindi में है · Read this in English →

'NoneType' object has no attributelist vs string methodsdir() से 10 सेकंड में जवाब

Message को दो हिस्सों में पढ़ो

AttributeError: 'str' object has no attribute 'append'

पहला हिस्सा बताता है चीज़ किस type की है ('str'), दूसरा बताता है आपने उस पर क्या माँगा ('append')। यानी आप string को list समझ बैठे थे। लगभग हर AttributeError इसी एक गलतफहमी से आता है: variable में वह चीज़ है ही नहीं जो आप सोच रहे थे।

इसीलिए fix की पहली line हमेशा यही है:

print(type(x), x)

कारण 1 — 'NoneType' object has no attribute (सबसे common)

Variable में None आ गया है। सबसे बड़ी वजह: वह function जो return करना भूल गया, या वह method जो जगह पर काम करके None लौटाती है।

def get_name():
    naam = "Priya"       # ⚠️ return नहीं किया

print(get_name().upper())   # ❌ 'NoneType' object has no attribute 'upper'

words = ["b", "a"]
words = words.sort()        # sort() None देता है
words.append("c")           # ❌ 'NoneType' object has no attribute 'append'

याद रखो — ये सब None लौटाते हैं: list.sort(), list.append(), list.reverse(), dict.update(), और बिना return वाला हर function। इनका result किसी variable में मत रखो।

कारण 2 — String methods को list पर (और उल्टा) लगाना

naam = "priya"
naam.append("!")     # ❌ 'str' object has no attribute 'append'
naam = naam + "!"    # ✅ strings immutable हैं — नई string बनती है

nums = [1, 2, 3]
nums.upper()         # ❌ 'list' object has no attribute 'upper'
nums.push(4)         # ❌ 'list' object has no attribute 'push' (वह JavaScript है!)
nums.append(4)       # ✅ Python में append

दूसरी language से आने वालों के लिए quick table: JS का push = Python का append; JS का length = Python का len(x); Java का size() = फिर से len(x)।

कारण 3 — Method पर brackets लगाना भूल गए

naam = "priya"
print(naam.upper)     # <built-in method upper of str object...> — call ही नहीं हुआ
print(naam.upper())   # ✅ PRIYA

यह AttributeError नहीं देता, पर output अजीब आता है और अगले step पर TypeError बन जाता है — इसलिए यहीं पकड़ लो।

कारण 4 — Module में वह function है ही नहीं

import math
print(math.square(4))    # ❌ module 'math' has no attribute 'square'
print(math.sqrt(4))      # ✅ 2.0

Trap: अपनी file का नाम math.py या random.py मत रखो — Python असली module की जगह आपकी file उठा लेगा और "has no attribute" देने लगेगा। file rename करो और साथ में बनी __pycache__ हटा दो।

10 सेकंड में जवाब — dir() और type()

naam = "priya"
print(type(naam))                                  # <class 'str'>
print([m for m in dir(naam) if not m.startswith("_")])
# ['capitalize', 'casefold', 'center', 'count', ... 'upper', 'zfill']

यही दो lines हर AttributeError का जवाब दे देती हैं: type() बताता है चीज़ है क्या, dir() बताता है उस पर क्या-क्या चलता है।

Class वाले code में एक और वजह होती है — __init__ में attribute set करना भूल जाना, या self. लगाना भूल जाना:

class Student:
    def __init__(self, naam):
        naam = naam          # ⚠️ self. गायब — attribute बना ही नहीं

s = Student("Priya")
print(s.naam)                # ❌ 'Student' object has no attribute 'naam'

class Student:
    def __init__(self, naam):
        self.naam = naam     # ✅

🛠️ Practice करो (free, browser में)

पढ़ना काफी नहीं — यही bug खुद fix करके देखो। नीचे के problems browser में ही चलते हैं, कोई install नहीं, कोई login नहीं।

सारे free problems देखो →

अक्सर पूछे जाने वाले सवाल

AttributeError का मतलब क्या होता है?

इसका मतलब है कि जिस object पर आपने कोई method या property माँगी, उस type पर वह मौजूद ही नहीं है — जैसे string पर append()। message का पहला हिस्सा type बताता है और दूसरा हिस्सा वह नाम जो नहीं मिला।

'NoneType' object has no attribute कैसे fix करें?

variable में None आ गया है। देखें कि उसे कौन बना रहा है — अक्सर कोई function return करना भूल गया है, या list.sort()/append() का result variable में रख दिया गया है (ये None लौटाते हैं)।

list में push() क्यों नहीं चलता?

push() JavaScript का method है। Python में एक item जोड़ने के लिए append() और कई items के लिए extend() इस्तेमाल करें।

किसी object के सारे methods कैसे देखें?

print(dir(obj)) चलाएँ — underscore से शुरू न होने वाले नाम ही आम methods हैं। साथ में print(type(obj)) से पक्का करें कि object उसी type का है जो आप मान रहे हैं।

आगे पढ़ो